In this article, we will show how to write a Python program to count and return the occurrence of the user-given element in a List with examples. While working with data, counting the occurrence of an element is the most common scenario to identify the frequent items. Otherwise, you can filter the unique items, etc.
In Python programming language, there are multiple options to count and return the occurrence of the user given element existing in a List. It includes the built-in functions and loops.
Using count() function
The count() function will count and return the occurrence of the given value in a list. In the program below, we have declared an integer list with some duplicate numbers. Next, we use the count() method to return how many times 1, 2, 4, and 5 numbers are repeated or occurred in a given list.
a = [1, 2, 1, 4, 5, 1, 2, 1, 4, 2, 1, 2, 4, 5]
print(a)
print(a.count(1))
print(a.count(2))
print(a.count(4))
print(a.count(5))
[1, 2, 1, 4, 5, 1, 2, 1, 4, 2, 1, 2, 4, 5]
5
4
3
2
Python Program to Count Occurrence of an Element in a List Using Counter
The collections module has the Counter class, and it can count the occurrence of all the elements in a given list and return the result in a dictionary object. Here, the dictionary key will be the list item, and the value is the total number of times the element has repeated in a list.
In the program below, first, we import the Counter() function from the collections library. Next, we used it on the integer list to count the occurrence of the element in a List.
from collections import Counter
a = [1, 2, 1, 4, 1, 5, 2, 1, 2, 1, 4, 2, 1, 2, 4, 5, 4]
print(a)
count = Counter(a)
print(count)
print(count[1])
print(count[2])
print(count[4])
print(count[5])
[1, 2, 1, 4, 1, 5, 2, 1, 2, 1, 4, 2, 1, 2, 4, 5, 4]
Counter({1: 6, 2: 5, 4: 4, 5: 2})
6
5
4
2
Using countOf()
The operator module has the countOf() function, which is equivalent to the traditional count() method that we discussed earlier. It accepts the sequence (list) after the first argument and the element to look for as the second argument. Next, it counts the total number of times the given element has repeated in a list and returns the output.
import operator
a = [1, 2, 1, 4, 1, 5, 2, 1, 2, 1, 4, 2, 1, 2, 4, 5, 4]
print(a)
num = int(input("Enter value = "))
count = operator.countOf(a, num)
print(num, "has repeated", count, "times")
[1, 2, 1, 4, 1, 5, 2, 1, 2, 1, 4, 2, 1, 2, 4, 5, 4]
Enter value = 1
1 has repeated 6 times
Python Program to Count Occurrence of an Element in a List Using the for loop
This approach is the native and popular approach because it gives more control to the user. The for loop in the below program will iterate each item in a given list. Next, the If statement compares each list element against the given value. If it found the match, the count variable will increase by 1.
a = [1, 2, 1, 4, 1, 5, 2, 1, 2, 1, 4, 2, 1, 2, 4, 5, 4]
print(a)
num = int(input("Enter value = "))
count = 0
for val in a:
if val == num:
count += 1
print(num, "has repeated", count, "times")
Using list comprehension
Similar to the above program, you can use list comprehension to count and return the occurrence of the user-given element in a list.
a = [1, 2, 1, 4, 1, 5, 2, 1, 2, 1, 4, 2, 1, 2, 4, 5, 4]
print(a)
num = int(input("Enter value = "))
count = [i for i in a if i == num]
print(num, "has repeated", len(count), "times")
[1, 2, 1, 4, 1, 5, 2, 1, 2, 1, 4, 2, 1, 2, 4, 5, 4]
Enter value = 4
4 has repeated 4 times
Python Program to Count Occurrence of an Element in a List Using enumerator
When you use the enumerator function, you can access each of the list values and its index.
a = [1, 2, 1, 4, 1, 5, 2, 1, 2, 1, 4, 2, 1, 2, 4, 5, 4]
print(a)
num = int(input("Enter value = "))
count = [val for i, val in enumerate(a) if val == num]
print(num, "has repeated", len(count), "times")
[1, 2, 1, 4, 1, 5, 2, 1, 2, 1, 4, 2, 1, 2, 4, 5, 4]
Enter value = 5
5 has repeated 2 times
Using Dictionary comprehension
This example uses the dictionary comprehension to find and return the total number of times the item has repeated in a given list.
a = [1, 2, 1, 4, 1, 5, 2, 1, 2, 1, 4, 2, 1, 2, 4, 5, 4]
print(a)
num = int(input("Enter value = "))
count = {val: a.count(val) for val in a}
print(num, "has repeated", count.get(num), "times")
[1, 2, 1, 4, 1, 5, 2, 1, 2, 1, 4, 2, 1, 2, 4, 5, 4]
Enter value = 1
1 has repeated 6 times