How to count total number of occurrences of an object in a Python list?

PythonServer Side ProgrammingProgramming

List is one of the most commonly used data structures provided by python. List is a data structure in python that is mutable and has an ordered sequence of elements. Following is a list of integer values āˆ’

Example

Following is a list of integer values.

lis= [1,2,7,5,9,1,4,8,10,1] print(lis)

Output

If you execute the above snippet, it produces the following output.

[1, 2, 7, 5, 9, 1, 4, 8, 10, 1]

In this article, we will discuss how to find the total count of occurrences of an object in a list in Python. For the above example, the occurrence of 1 is 3.

Python includes several methods for counting the number of times a list item appears.

Using loops

In this method, we use a traditional approach where we iterate over a loop and have a counter variable to count the occurrence of the item.

Example

In the below example we take the input from the user and count the occurrence of that required element.

def count_occurrence(lst, x): count = 0 for item in lst: if (item == x): count = count + 1 return count lst =[] n = int(input("Enter number of elements and the elements: ")) for i in range(n): item = int(input()) lst.append(item) x = int(input("Enter the number whose count you want to find in the list: ")) y = count_occurrence(lst,x) print('The element %s appears %s times in the list'%(x,y))

Output

The output of the above code is as follows.

Enter number of elements and the elements: 8
2
4
1
3
2
5
2
9
Enter the number whose count you want to find in the list: 2
The element 2 appears 3 times in the list

Using the list count() method

The count() method counts the number of times an element appears in the list. In this method, we use the python count() method to count the occurrence of an element in a list. This method calculates the total occurrence of a given element of a list.

Syntax

The syntax of the count() method is as follows.

list.count(ele)

Where, ele is the element to be counted.

Example

In the following example we count the number of times ā€˜iā€™ is occurred in the vowel list using the count() method.

vowels = ['a', 'e', 'i', 'o', 'i', 'u', 'i', 'o', 'e', 'a'] count = vowels.count('i') print("The count is" ,count)

Output

The output of the above code is as follows.

The count is 3

Using the counter() method

This is another way to count the occurrence of an element in a list. The counter method provides a dictionary containing all element occurrences as a key value pair, where the key is the element, and the value is the number of times it has occurred. We need to import the Counter module from collections.

Example

In this example, we use the counter() method to count the occurrence of an element in a list.

from collections import Counter l = [1,2,7,5,9,1,4,8,10,1] x = 1 d = Counter(l) print('{} has occurred {} times'.format(x, d[x]))

Output

The output of the above code is as follows.

1 has occurred 3 times
raja
Updated on 08-Nov-2022 12:48:44

Advertisements