How can I count the occurrences of a list item? How can I count the occurrences of a list item? python python

How can I count the occurrences of a list item?


If you only want one item's count, use the count method:

>>> [1, 2, 3, 4, 1, 4, 1].count(1)3

Important Note regarding count performance

Don't use this if you want to count multiple items.

Calling count in a loop requires a separate pass over the list for every count call, which can be catastrophic for performance.

If you want to count all items, or even just multiple items, use Counter, as explained in the other answers.


Use Counter if you are using Python 2.7 or 3.x and you want the number of occurrences for each element:

>>> from collections import Counter>>> z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']>>> Counter(z)Counter({'blue': 3, 'red': 2, 'yellow': 1})


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_functionimport timeitt1=timeit.Timer('Counter(l)', \                'import random;import string;from collections import Counter;n=1000;l=[random.choice(string.ascii_letters) for x in range(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 range(n)]'                )print("Counter(): ", t1.repeat(repeat=3,number=10000))print("count():   ", t2.repeat(repeat=3,number=10000)

And the output:

Counter():  [0.46062711701961234, 0.4022796869976446, 0.3974247490405105]count():    [7.779430688009597, 7.962715800967999, 8.420845870045014]