Maybe using list.count() you can achieve the same with less lines? x = [1,1,2,3,1,3] def times_so_far(ls): out = [0]*len(ls) for i in xrange(len(ls)): out[i] = ls[:i].count(ls[i]) return out This can be written with list comprehension as mentioned by Caridorc, and it removes the first `len(ls)` call this way (though maybe at the cost of resizing `out` for every element of the list): def times_so_far(ls): return [ls[:i].count(ls[i]) for i in xrange(len(ls))] Now, if you're going to work with lists of positive integers of a small value, I would recommend the following, as it's similar to yours but it works with indices instead of dict keys: def times_so_far(ls, maxValue): temp = [0]*(maxValue+1) out = [0]*len(ls) for i in xrange(len(ls)): out[i] = temp[ls[i]] temp[ls[i]] += 1 return out