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 10 appears three times in given list. Input : lst = [8, 6, 8, 10, 8, 20, 10, 8, 8] x = 16 Output : 0
Method 1: Count Occurrences of Element in Python 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 occurrences def countX(lst, x): count = 0 for ele in lst: if (ele = = x): count = count + 1 return count # Driver Code lst = [ 8 , 6 , 8 , 10 , 8 , 20 , 10 , 8 , 8 ] x = 8 print ( '{} has occurred {} times' . format (x, countX(lst, x))) |
Output:
8 has occurred 5 times
Method 2: Count Occurrences of Element in Python 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 occurrences def countX(lst, x): return lst.count(x) # Driver Code lst = [ 8 , 6 , 8 , 10 , 8 , 20 , 10 , 8 , 8 ] x = 8 print ( '{} has occurred {} times' . format (x, countX(lst, x))) |
Output:
8 has occurred 5 times
Method 3: Count Occurrences of Element in Python 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 list l = [ 1 , 1 , 2 , 2 , 3 , 3 , 4 , 4 , 5 , 5 ] # driver program x = 3 d = Counter(l) print ( '{} has occurred {} times' . format (x, d[x])) |
Output:
3 has occurred 2 times
Method 4: Count Occurrences of Element in Python List using countof() method
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 list l = [ 1 , 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 5 , 5 ] # driver program x = 2 print (f "{x} has occurred {op.countOf(l, x)} times" ) |
Output:
2 has occurred 3 times
Method 5: Count the Number of Occurrences using a 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' )) |
Output:
1
Method 6: 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 list l = [ 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