Python | Count occurrences of an element in a list
Given a list in Python and a number x, count the 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 Explanation: 10 appears three times in given list. Input: lst = [8, 6, 8, 10, 8, 20, 10, 8, 8], x = 16 Output: 0 Explanation: 16 appears zero times in given list.
Method 1: Count occurrences of an element in a list Using a Loop in Python
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))) |
8 has occurred 5 times
Method 2: Count occurrences of an element in a list Using count()
The idea is to use the list method count() to count the 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))) |
8 has occurred 5 times
Method 3: Count occurrences of an element in a list Using Counter()
The counter method returns a dictionary with occurrences of all elements as a key-value pair, where the key is the element and the 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])) |
3 has occurred 2 times
Method 4: Count occurrences of an element in a list Using countof()
Operator.countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value. It returns the Count of a number of occurrences of value.
Python3
import operator as op# declaring the listl = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5]# driver programx = 2print(f"{x} has occurred {op.countOf(l, x)} times") |
2 has occurred 3 times
Method 5: Count occurrences of an element in a list Using dictionary comprehension
Python allows dictionary comprehensions. We can create dictionaries using simple expressions. A dictionary comprehension takes the form {key: value for (key, value) in iterable}
Python3
lis = ['a', 'd', 'd', 'c', 'a', 'b', 'b', 'a', 'c', 'd', 'e']occurrence = {item: lis.count(item) for item in lis}print(occurrence.get('e')) |
1
Method 6: Count occurrences of an element in a list Using the panda’s library
Pandas Series.value_counts() function return a Series containing counts of unique values. The resulting object will be in descending order so that the first element is the most frequently-occurring element.
Python3
import pandas as pd# declaring the listl = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5]count = pd.Series(l).value_counts()print("Element Count")print(count) |
Output:
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int64
Method: Using list comprehension
Python3
l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5]ele=1x=[i for i in l if i==ele]print("the element",ele,"occurs",len(x),"times") |
the element 1 occurs 2 times
Method: Using enumerate function
Python3
l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5]ele=1x=[i for j,i in enumerate(l) if i==ele]print("the element",ele,"occurs",len(x),"times") |
the element 1 occurs 2 times






Please Login to comment...