Python count occurrences in list (In 4 Ways)

python count occurrences in list
Python count occurrences of element in list

In this tutorial, we will be solving a program for python count occurrences of element in list. There are many ways to solve this problem out of which we will be solving this particular problem in 4 ways.

Method 1- Using List count() to count occurrences in list

The simple way to get the count of occurrences of elements in the list is by using the list count() method. List count() takes an element as input and returns the count of occurrences of that element present in the list.

The complexity of the python list count() function is O(N), where N is the number of elements present in the list.

Syntaxlist.count(element)
Python code: 

# sample list
sample_list = [3,7,8,9,3,6,7,3,10,17,10,3]
#using count()
count = sample_list.count(3)
print("Count of 3 in sample list is:", count)

Output:

Count of 3 in sample list is: 4

Method 2- Using countOf() method of Opertor module to count occurrences of element in list

Python Operator module defines the efficient functions corresponding to the mathematical, logical, relational, bitwise, etc operations. To know more about the operator module visit here.

countOf() function from the operator module can be used to get the occurrences of the element in the list.

Syntax- operator.countOf(a, b)

Return the number of occurrences of b in a.

Python Code:

# import Operator module
import operator
# sample list
sample_list = [3,7,8,9,3,6,7,3,10,17,10,3]
# using counter
count = operator.countOf(sample_list, 3)
print("Count of 3 in sample list is:", count)

Output:

Count of 3 in sample list is: 4

Method 3- Using Counter from Collections module to count occurrences in list

A Counter is a dict subclass for counting hashable objects. It is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values.

Counter takes an iterator as input and creates a dictionary, in which keys are taken as distinct elements from that input iterator, and values of those keys are taken as the number of occurrences of that key in the input iterator.

Python Code:

from collections import Counter
# sample list
sample_list = [3,7,8,9,3,6,7,3,10,17,10,3]
# Using Counter
d = Counter(sample_list)
print("Dictionary Created is", d)
# acessing the dictionary to get specific element count
print("Count of 3 in sample list is:", d[3])

Output:

Dictionary Created is Counter({3: 4, 7: 2, 10: 2, 8: 1, 9: 1, 6: 1, 17: 1})

Count of 3 in sample list is: 4

Method 4- Using For loop and set() to count occurrences of element in list

Steps to get the occurrences of the element in a list by using for loop and set() are-

  1. Convert list to set to get distinct elements.
  2. Loop on set and will count occurrences of element present in list.

Python code:

# sample list
sample_list = [3,7,8,9,3,6,7,3,10,17,10,3]
# element to count from list
ele = 3
#initialize the counter
count = 0
# using for loop
for i in sample_list:
    if i == ele:
        count += 1
print("Count of {0} in sample list is: {1}".format(ele, count))

Output:

Count of 3 in sample list is: 4

How to get count occurrences of all elements in the list

To get occurrences of elements we can use a dictionary. The simple approach to get the frequency of all elements by using a dictionary is to iterate over the list and make the distinct element as a key of the dictionary and store the corresponding count of that key as values.

The time complexity to get the count of occurrences of all elements in the list using this approach is O(N), where N is the length of the list.

Python code:

# sample list
sample_list = [3,7,8,9,3,6,7,3,10,17,10,3]
# define empty dictionary
occurrences = {}
# Checking the element from sample list present as key in dictionary
# if yes than increment by 1 or create new key with value 1
for i in sample_list:
    if i in occurrences:
        occurrences[i] += 1
    else:
        occurrences[i] = 1
# Printing dictionary
print("element count using dictionary",occurrences)
# Printing all element with its count of Occurrence
for key,value in occurrences.items():
    print("The Occurrence of {0} in the sample list is: {1}".format(key, value))

Output:

element count using dictionary {3: 4, 7: 2, 8: 1, 9: 1, 6: 1, 10: 2, 17: 1}
The Occurrence of 3 in the sample list is: 4
The Occurrence of 7 in the sample list is: 2
The Occurrence of 8 in the sample list is: 1
The Occurrence of 9 in the sample list is: 1
The Occurrence of 6 in the sample list is: 1
The Occurrence of 10 in the sample list is: 2
The Occurrence of 17 in the sample list is: 1

Conclusion

Hence we can count occurrences of elements in the list in python using count(), operator module, Counter, and using set() and the for loop. 

Similar Posts