Count the number of occurrences of an Element in the python List

In this tutorial we will be focusing on how to count the number of occurrences of an element in the python list. We will be trying out different ways to count the occurrence of an element in the python. one of them being using count() method and other is to use the function like counter() , countOf() etc.

  • Count the number of occurrences of an element in the python list using count() method.
  • Count the number of occurrences of an element in the python list using counter() method.
  • Count the number of occurrences of all the elements in the python list using counter() method
  • Count the number of occurrences of all the elements in the python list using dictionary comprehension.
  • Using pandas value_counts() to count the occurrence of an element in the python list
  • Count the number of occurrences of an element in the python list using countOf() method.
  • Count the number of occurrences of an element in the python list using dictionary comprehension.
  • Python user defined function to count the number of occurrences of an element

 

Count-number-of-occurrence-in-python-list-3

 

count() function to count the number of occurrence of an element in the python list:

Example 1:

We will be using count() function which takes the element of a list as an argument and then it calculates the number of occurrence of that particular list as shown below.

# Count the Number of Occurrences in a Python list using .count()

List = ['a', 'e', 'i', 'o', 'i', 'u', 'e', 'i']

count_e = List.count('e')

print(count_e)

so, the output will be,

output:

2

Example 2:

Count of occurrence of the element ‘w’ is attempted below as ‘w’ is not present in the given list it will return 0

# Count the Number of Occurrences in a Python list using .count() when

List = ['a', 'e', 'i', 'o', 'i', 'u', 'e', 'i']

count_w = List.count('w')

print(count_w)

so, the output will be,

output:

0

 

 

 

counter() function to count the number of occurrence of an element in the python list:

Example 1:

counter() function takes the entire list as an argument and then calculates the count of occurrence of all the elements of that list, then we will printing the count of any particular item in the list as shown below.

from collections import Counter

List = ['a', 'e', 'i', 'o', 'i', 'u', 'e', 'i']

counts = Counter(List)

print(counts['i'])

so, the output will be,

output:

3

 

Example 2:

Count of occurrence of the element ‘w’ is attempted below as ‘w’ is not present in the given list it will return 0

# # Count the Number of Occurrences in a Python list using Counter

from collections import Counter

List = ['a', 'e', 'i', 'o', 'i', 'u', 'e', 'i']

counts = Counter(List)

print(counts['w'])

so, the output will be,

output:

0

 

 

 

Count the number of occurrences of all the elements in the python list using counter() method :

counter() function takes the entire list as an argument and then calculates the count of occurrence of all the elements of that list

from collections import Counter

List = ['a', 'e', 'i', 'o', 'i', 'u', 'e', 'i']

print(Counter(List))

so, the output will be,

output:

Counter({‘i’: 3, ‘e’: 2, ‘a’: 1, ‘o’: 1, ‘u’: 1})

 

 

 

Count the number of occurrences of an element in the python list using dictionary comprehension:

Python dictionary comprehensions are powerful tools that let us generate new dictionaries by looping over an iterable item. In this case, we will loop over a Python list and generate a dictionary that allows us to count how many times each item appears in a list which will result in the dictionary of elements and count of occurrence of all the elements as shown below

 

# # Count the Number of Occurrences in a Python list using a dictionary comprehension

List = ['a', 'e', 'i', 'o', 'i', 'u', 'e', 'i']

counts = {item:List.count(item) for item in List}

print(counts)

so, the output will be the dictionary

output:

{‘a’: 1, ‘e’: 2, ‘i’: 3, ‘o’: 1, ‘u’: 1}

 

 

 

Using pandas value_counts() to count the occurrence of an element in the python list:

We will be converting the list to pandas series and will be using value_counts() function to calculates the count of occurrence of an element in the list as shown below.

import pandas as pd

List = ['a', 'e', 'i', 'o', 'i', 'u', 'e', 'i']

counts = pd.Series(List).value_counts()

print(counts.get('i'))

# Returns: 3

so, the output will be,

output:

3

 

 

 

countOf() function to count the number of occurrence of an element in the python list:

We will be using countOf() function of the operator library to count the number of times an item appears in a list. The library comes with a helpful function, countOf(), which takes two arguments:

  • The list to use to count items
  • The item to count

 

# Count the Number of Occurrences in a Python list using operator

from operator import countOf

List = ['a', 'e', 'i', 'o', 'i', 'u', 'e', 'i']

count_i = countOf(List, 'i')

print(count_i)

so, the output will be,

output:

3

 

 

 

count the number of occurrence of an element in the python list using for loop:

We will be using for loop to iterate through the list and creating an if condition on satisfaction of the condition we will be incrementing the count there by calculating the count of the occurrence of an item appearing in a list.

 

# # Count the Number of Occurrences in a Python list using a For Loop
List = ['a', 'e', 'i', 'o', 'i', 'u', 'e', 'i']
count = 0
for item in List:
    if item == 'i':
        count += 1
print(count)

so, the output will be,

output:

3

 

 

 

 

Count the number of occurrences of an element in the python list using dictionary comprehension:

Python dictionary comprehensions are powerful tools that let us generate new dictionaries by looping over an iterable item. In this case, we will loop over a Python list and generate a dictionary that allows us to count how many times an item appears in a list.

# # Count the Number of Occurrences in a Python list using a dictionary comprehension

List = ['a', 'e', 'i', 'o', 'i', 'u', 'e', 'i']

counts = {item:List.count(item) for item in List}

print(counts.get('i'))

so, the output will be

output:

3

 

 

 

Python user defined function to count the number of occurrences of an element:

Under the user defined function, we will be using for loop to iterate through the list and creating an if condition on satisfaction of the condition we will be incrementing the count there by calculating the count of the occurrence of an item appearing in a list.

# Python code to count the number of occurrences
def countX(List, x):
    count = 0
    for ele in List:
        if (ele == x):
            count = count + 1
    return count
 
# Driver Code
List = ['a', 'e', 'i', 'o', 'i', 'u', 'e', 'i']
x = 'i'
print(countX(List, x))

so, the output will be

output:

3

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.