Count Occurrences of Items in a Python List Using counter()

(Last Updated On: 08/03/2023)

Python Program to Count Occurrences of Items in a Python List Using counter()

# Count the Occurrence of an Item in a List Using Counter() in python

from collections import Counter

# declaring the list
l = [1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 3, 3]

# driver program
x = 3
d = Counter(l)
print('{} has occurred {} times'.format(x, d[x]))

Output:

3 has occurred 5 times

Leave a Reply

Your email address will not be published. Required fields are marked *