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))]