Counting the occurrences of one item in a list
For counting the occurrences of just one list item you can use count()
>>> l = ["a","b","b"] >>> l.count("a") 1 >>> l.count("b") 2 Counting the occurrences of all items in a list is also known as "tallying" a list, or creating a tally counter.
Counting all items with count()
To count the occurrences of items in l one can simply use a list comprehension and the count() method
[[x,l.count(x)] for x in set(l)] (or similarly with a dictionary dict((x,l.count(x)) for x in set(l)))
Example:
>>> l = ["a","b","b"] >>> [[x,l.count(x)] for x in set(l)] [['a', 1], ['b', 2]] >>> dict((x,l.count(x)) for x in set(l)) {'a': 1, 'b': 2} Counting all items with Counter()
Alternatively, there's the faster Counter class from the collections library
Counter(l) Example:
>>> l = ["a","b","b"] >>> from collections import Counter >>> Counter(l) Counter({'b': 2, 'a': 1}) How much faster is Counter?
I checked how much faster Counter is for tallying lists. I tried both methods out with a few values of n and it appears that Counter is faster by a constant factor of approximately 2.
Here is the script I used:
from __future__ import print_function import timeit t1=timeit.Timer('Counter(l)', \ 'import random;import string;from collections import Counter;n=1000;l=[random.choice(string.ascii_letters) for x in xrangerange(n)]' ) t2=timeit.Timer('[[x,l.count(x)] for x in set(l)]', 'import random;import string;n=1000;l=[random.choice(string.ascii_letters) for x in xrangerange(n)]' ) print ("Counter(): ", t1.repeat(repeat=3,number=10000)) print ("count(): ", t2.repeat(repeat=3,number=10000) And the output:
Counter(): [6[0.36064818714657946062711701961234, 60.6138815596997564022796869976446, 60.392260466851987]3974247490405105] count(): [12[7.885062765334006779430688009597, 137.045601897769359962715800967999, 128.87746743077426]420845870045014]