I have a list which has repeating items and I want a list of the unique items with their frequency.
For example, I have [\'a\', \'a\', \'b\', \'b\', \'b\']
Another way to do this would be
mylist = [1, 1, 2, 3, 3, 3, 4, 4, 4, 4]
mydict = {}
for i in mylist:
if i in mydict: mydict[i] += 1
else: mydict[i] = 1
then to get the list of tuples,
mytups = [(i, mydict[i]) for i in mydict]
This only goes over the list once, but it does have to traverse the dictionary once as well. However, given that there are a lot of duplicates in the list, then the dictionary should be a lot smaller, hence faster to traverse.
Nevertheless, not a very pretty or concise bit of code, I'll admit.