Given a list in Python and a number x, count number of occurrences of x in the given list.
Examples:
Input : lst = [15, 6, 7, 10, 12, 20, 10, 28, 10]
x = 10
Output : 3
10 appears three times in given list.
Input : lst = [8, 6, 8, 10, 8, 20, 10, 8, 8]
x = 16
Output : 0
Method 1 (Simple approach)
We keep a counter that keeps on increasing if the required element is found in the list.
Python3
# Python code to count the number of occurrencesdef countX(lst, x): count = 0 for ele in lst: if (ele == x): count = count + 1 return count# Driver Codelst = [8, 6, 8, 10, 8, 20, 10, 8, 8]x = 8print('{} has occurred {} times'.format(x, countX(lst, x))) |
Output: 8 has occurred 5 times
Method 2 (Using count())
The idea is to use list method count() to count number of occurrences.
Python3
# Python code to count the number of occurrencesdef countX(lst, x): return lst.count(x)# Driver Codelst = [8, 6, 8, 10, 8, 20, 10, 8, 8]x = 8print('{} has occurred {} times'.format(x, countX(lst, x))) |
Output: 8 has occurred 5 times
Method 2 (Using Counter())
Counter method returns a dictionary with occurrences of all elements as a key-value pair, where key is the element and value is the number of times that element has occurred.
Python3
from collections import Counter# declaring the listl = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]# driver programx = 3d = Counter(l)print('{} has occurred {} times'.format(x, d[x])) |
Output: 3 has occurred 2 times
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


