Which method returns the number of occurrences of an element in a list?

Python | Count occurrences of an element in a list

Given a list in Python and a number x, count 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

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1 (Simple approach)
We keep a counter that keeps on increasing if the required element is found in the list.




# 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 (Using count())
The idea is to use list method count() to count number of occurrences.




# 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 2 (Using Counter())
Counter method returns a dictionary with occurrences of all elements as a key-value pair, where key is the element and value is the number of times that element has occurred.




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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python list-programs
python-list
Practice Tags :
python-list

Count Occurrences of Element in Python List

Which method returns the number of occurrences of an element in a list?

Python is well known for its easy syntax, fast implementation, and, most importantly, large support of multiple data structures. Lists are one of those data structures in python which helps to store large amounts of sequential data in a single variable. As huge data is stored under the same variable, it is sometimes quite difficult to manually identify whether the given element is present in the lists, and if yes, how many times. Therefore, in this article, we will study the various ways to count the number of occurrences in the list in python. To recall the concepts of python lists in detail, visit our article “3 Ways to Convert List to Tuple”.

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)

Example 1:

Here we discussed the most efficient way to get the total count of occurrences of items in the list by using the list count() method. The function count() takes a value as an input, and after calculating the occurrences of that element, it returns an integer value.

To write a program for the count() function, we use the Spyder compiler so that you can understand how it calculates the occurrence of a number. We first create a new blank file and then start writing python code. Initially, we create a python list and add values to it. Then we use a list count and print function that calculates an element’s number of occurrences and then displays its result in the form of an integer value.

My_list = [ 3, 4, 8, 4, 0, 6, 1, 3, 11, 17, 11, 4]

Count = My_List.count(3)

Print(“Occurrence of 4 in my list is:”, count)

Which method returns the number of occurrences of an element in a list?

We save our source file and specify its name “PythonCountOccurrences.”

Which method returns the number of occurrences of an element in a list?

After this, run the file and check count() calculates the existence of an element in the list.

Which method returns the number of occurrences of an element in a list?

methods to count occurences

  • Method 1: Counting Occurrences using builtin count() function.
  • Method 2: Using loops to count unique values and elements in the list.
  • Method 3: Using Counter class.

Using built-in count() function to count occurrences in a list

The python has a built-in count() function which takes a value as an argument whose count you want to find in the list. It will return an integer value that represents the number of times a specified item occurs in the list. If the integer does not exist, it returns 0.

following is the code To count occurences of an element in a list in python

# Create a list of integers list1 = [1, 2, 2, 7, 2, 9, 1, 4, 3] x = list1.count(2) print("The number 2 occurs " + str(x) + " times in the list.") Output: The number 2 occurs 3 times in the list.
# Create a list of integers list1 = [1, 2, 2, 7, 2, 9, 1, 4, 3] x = list1.count(2) print("The number 2 occurs " + str(x) + " times in the list.") Output: The number 2 occurs 3 times in the list.

Example 2:

Similarly, if you apply the same method to determine the same elements in strings.

# Create a list of Strings list1 = ['mango', 'mango', 'guava', 'apple', 'guava', 'mango', 'apple'] x = list1.count('apple') print("The Fruit 'apple' occurs " + str(x) + " times in the list.") x = list1.count('orange') print("The Fruit 'orange' occurs " + str(x) + " times in the list.")
output of program to count occurrences in list of python using built in function count()