Signup/Sign In

Python Program to Count occurrences of an element in a list

In this tutorial, you will learn to count occurrences of an element in a list in Python. The List is an ordered set of values enclosed in square brackets [ ]. List stores some values called elements in it, which can be accessed by their particular index.

Occurrence simply means the total number of times an element is present in the list.

An element in a list can be repeated many times in the list. We have to write a program that will input an element from the user and then count and display the number of times that element is repeated in the list.

Input: list=[2, 4, 1, 7, 9, 10, 12, 17, 1] n=1

Output: 2 ( 1 occurs 2 times in the list)

Input: list=[3, 4, 5, 7, 8, 10, 2] n=1

Output: 0 (1 occurs 0 times in the list)

Approach

For writing a program that can count the number of occurrences we can follow these approaches-

  1. using loop and counter variable
  2. using count() function
  3. using Counter() function

Approach 1: loop and counter variable

This is a naive approach for solving the given problem. We will use a loop to access each element in the list and check if it is the element given by the user. When the element is found in the list, the value of the counter variable will be updated. Display the value of the counter variable as the final result.

Algorithm

Follow the algorithm to understand the approach better:

Step 1- Define a function to count the occurrence of an element

Step 2- Inside the function, declare a counter variable

Step 3- un a loop to access each element and check for the occurrence

Step 4- If found increase the counter variable by 1

Step 5- Return the counter variable

Step 6- Take input of list from user

Step 7- Take input of element whose occurrence has to be checked in the list

Step 8- Call function and pass the list and element

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach. In this program, we have defined a function that will check the occurrence of an element in the list and with the help of a counter variable, we can track the number of times that element is present in the list.

#occurrences in list

#function for counting
def count_occurrence(list, n):
    
#counter variable
    count=0
    for i in list:
        if(i==n):
          #update counter variable
            count=count+1
    return count

#input list
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
    e=int(input("Enter element of list "))
    li.append(e)
print("Original list: ",li)

x=int(input("Enter element to be checked list: "))

#function call
print(x," has occurred ",count_occurrence(li, x),"times")


Enter size of list 6
Enter element of list 2
Enter element of list 3
Enter element of list 1
Enter element of list 2
Enter element of list 4
Enter element of list 2
Original list: [2, 3, 1, 2, 4, 2]
Enter element to be checked list: 2
2 has occurred 3 times

Approach 2: using count() function

count() is an in-built function in Python that accepts a number and returns the occurrence of that number in the list.

Algorithm

Follow the algorithm to understand the approach better

Step 1- Take input of list from user

Step 2- Take input of number whose occurrence has to be checked in the list

Step 3- Use count() to calculate the occurrence of a number

Step 4- Print the result

Python Program 2

Look at the program to understand the implementation of the above-mentioned approach. In this program, we have used the count() method to calculate the occurrence of the element.

#occurrences in list
#count()

#input list
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
    e=int(input("Enter element of list "))
    li.append(e)
print("Original list: ",li)

n=int(input("Enter element to be checked list: "))

#using count()
print(n," has occurred ",li.count(n),"times")


Enter size of list 5
Enter element of list 1
Enter element of list 2
Enter element of list 3
Enter element of list 1
Enter element of list 2
Original list: [1, 2, 3, 1, 2]
Enter element to be checked list: 1
1 has occurred 2 times

Approach 3: using Counter() function

The Counter() is a built-in function that returns a dictionary of all the elements and their occurrences in a list. A dictionary has keys and values corresponding to it, the elements in the list and their occurrences are returned in a similar way.

Algorithm

Follow the algorithm to understand the approach better

Step 1- Take input of list from user

Step 2- Take input of number whose occurrence has to be checked in the list

Step 3- Use Counter() to calculate occurrences of all the numbers in the list

Step 4- Use the element as a key and access the value corresponding to it

Step 5- Print the value as the occurrence.

Python Program 3

Look at the program to understand the implementation of the above-mentioned approach. In this program, we have used the Counter() method to calculate the occurrence of all the elements in the list.

To get the occurrence of a single element, use the element as the key and display the value corresponding to it. This will be the occurrence of that element.

To use the Counter() function, import it from the collections module.

#occurrences in list
#Counter()

from collections import Counter
 
# declaring the list
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
    e=int(input("Enter element of list "))
    li.append(e)
print("Original list: ",li)

# driver program
x = int(input("Enter element to be checked list: "))
d = Counter(li)
print('{} has occurred {} times'.format(x, d[x]))


Enter size of list 4
Enter element of list 3
Enter element of list 2
Enter element of list 3
Enter element of list 2
Original list: [3, 2, 3, 2]
Enter element to be checked list: 3
3 has occurred 2 times

Conclusion

So far, we have discussed three methods for finding the occurrence of an element in a list. We have discussed a simple approach, and using built-in functions like count() and Counter() for calculating the occurrence of an element in Python