from bisect import bisect as bs dictA = dict.fromkeys(xrange(1,48), dict.fromkeys(xrange(1,8),0)) # The way I prefer to do it. dictB = {} # The way that works. for x in xrange(1,48): dictB[x] = {1:0,2:0,3:0,4:0,5:0,6:0,7:0} listOfData = [] # Contains the data Ex: (1,12,18,36,40,45,47) for x in xrange(1,48): for row in listOfData: if x in row: dictA[x][bs(row,x)] += 1 dictB[x][bs(row,x)] += 1 """ DictB returns the correct values. DictA returns the total number of rows in the listOfData """