How to count the occurrences of list items in Python?
Use the list.count() method of the built-in list class to get the number of occurrences of an item in the given list.
names=['Deepak','Reema','John','Deepak','Munna','Reema','Deepak','Amit','John','Reema']
nm=input('Enter name to count: ')
count=names.count(nm)
print('count = ', count)
Enter name to count: John count = 2
Count Occurences of Each Item in List
We need two nested for loops to count the occurrences of each item in the list. Name at i'th
positional index - i
being variable controlling outer loop, is searched for occurrence in remaining list iterated with j variable. The current name and its count are added in a dictionary, only if the name is not previously present in it as a key.
names=['Deepak','Reema','John','Deepak','Munna','Reema','Deepak','Amit','John','Reema']
<pre className="language-python"><code>names=['Deepak','Reema','John','Deepak','Munna','Reema','Deepak','Amit','John','Reema']
d={}
for i in range(len(names)-1):
x=names[i]
c=0
for j in range(i,len(names)):
if names[j]==names[i]:
c=c+1
count=dict({x:c})
if x not in d.keys():
d.update(count)
print (d)
{'Deepak': 3, 'Reema': 3, 'John': 2, 'Munna': 1, 'Amit': 1}
Again, we can use the count() method to make the solution more concise.
names=['Deepak','Reema','John','Deepak','Munna','Reema','Deepak','Amit','John','Reema']
<pre className="language-python"><code>names=['Deepak','Reema','John','Deepak','Munna','Reema','Deepak','Amit','John','Reema']
d={}
for i in range(len(names)-1):
x=names[i]
c=0
for j in range(i,len(names)):
c=names.count(x)
count=dict({x:c})
if x not in d.keys():
d.update(count)
print (d)
{'Deepak': 3, 'Reema': 3, 'John': 2, 'Munna': 1, 'Amit': 1}
However, this solution is not an efficient one. The occurrence of an item is counted as many times it appears in the list, thereby increasing the processing time, especially for a list with a large number of items.
To counter this problem, we first create a collection of unique items in the given list. Pythton's Set object is a collection of unique items.
names=['Deepak','Reema','John','Deepak','Munna','Reema','Deepak','Amit','John','Reema']
<pre className="language-python"><code>names=['Deepak','Reema','John','Deepak','Munna','Reema','Deepak','Amit','John','Reema']
nameset=set(names)
print(nameset)
{'Reema', 'Munna', 'Amit', 'Deepak', 'John'}
Now we can count the occurrence of a set items in the list and construct the dictionary object. However, this time we will use list comprehension instead of regular for loop as we used above.
names=['Deepak','Reema','John','Deepak','Munna','Reema','Deepak','Amit','John','Reema']
nameset=set(names)
d={name:names.count(name) for name in nameset}
print(d)
{'Reema': 3, 'Munna': 1, 'Amit': 1, 'Deepak': 3, 'John': 2}
Count Occurences of Each Item in List using Collection Module
The collection module in Python's standard library defines many specialized container data types. One of them is a Counter class. It is a dict subclass that helps in the giving count of the hashable items (such as list or dictionary).
from collections import Counter
names=['Deepak','Reema','John','Deepak','Munna','Reema','Deepak','Amit','John','Reema']
d=dict(Counter(names))
print (d)
<pre className="language-python"><code>from collections import Counter
names=['Deepak','Reema','John','Deepak','Munna','Reema','Deepak','Amit','John','Reema']
d=dict(Counter(names))
print (d)
{'Deepak': 3, 'Reema': 3, 'John': 2, 'Munna': 1, 'Amit': 1}
Note that, above, a Counter
object is typecasted to a regular dictionary object to display the result.