Open In App

Python | Count occurrences of an element in a list

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Count Occurrences of Item in Python List

Below are the methods by which we can count all occurrences of an element in a Python List.

Python Count occurrences 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

Python Count Occurences using List Comprehension

In this example, we are using list comprehension to count all occurrences of an element in a Python list.

Python3




l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5]
ele=1
x=[i for i in l if i==ele]
print("the element",ele,"occurs",len(x),"times")


Output

the element 1 occurs 2 times

Python Count using Enumerate Function

In this example, we are using enumerate function to count all occurrences of an element in a Python list.

Python3




l = [1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5]
ele=1
x=[i for j,i in enumerate(l) if i==ele]
print("the element",ele,"occurs",len(x),"times")


Output

the element 1 occurs 2 times

Count occurrences of an element 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

Python 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 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

Count occurrences of an element 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 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

Using Python 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

Using the Pandas 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


Last Updated : 24 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads