Count the number of occurrences of a list item

How to count the number of occurrences of a list item in Python ?

This tutorial guides you on how to count the number of occurrences of a list item using Python programming. Let’s learn various ways to find the number of occurrences of a list item in Python with examples.

Count the number of occurrences of a list item

Count the number of occurrences of a list item

There are numerous options available in Python programming to count the number of occurrences of each list item. In the following section we are going to see how to use list.count() method of the built-in list class of Python.

Using list.count(x)

In the following example, we are using list.count(x). The list data type has method called count(x) which will return the number of times the x is occurring in the given list.

For example, the given list.

mylist = [10, 20, 10, 40, 50, 11, 22, 33, 50, 55, 66, 40]

The following Python function uses list’s method count(x).

def getCount(list, x):
    return list.count(x)

To find out how many times the item ‘10‘ is repeated in the list.

getCount(mylist, 10)

Output

2

Using for loop and counter

Use count variable to record the number of occurrences of a given element and for loop for iterating the given list.

mylist = [10, 20, 10, 40, 50, 11, 22, 33, 50, 55, 66, 40]

For example,

def getCount(list, x):
    count = 0
    for element in list:
        if (element == x):
            count = count + 1
    return count

To find out number of times the item ‘11‘ occurred in the list.

getCount(mylist, 11)

Output

1

Using collections Counter

You can also use Counter class from collections library to count occurrences for all the items in the list at one shot.

For example, to count occurrences of each item in the list using Counter class.

from collections import Counter
Counter(mylist)

Output

Counter({10: 2, 20: 1, 40: 2, 50: 2, 11: 1, 22: 1, 33: 1, 55: 1, 66: 1})

Using Set, dictionary and list comprehension

In this approach, first you will create collection of unique items of the given list using set.

For example, mylist is the given input list and myset is the collection of unique items of the list.

myset = set(mylist)

Output

When you try to print elements of myset, you will get the following output.

{33, 66, 40, 10, 11, 50, 20, 22, 55}

After creating the set items. Now, you can count the occurrence of set items in the list and create a dictionary object. And let’s use list comprehension instead of regular for loop as shown in the following example.

myset = set(mylist)
result={item:mylist.count(item) for item in myset}
print(result)

Output

When you run the above program, you will get the following output.

{33: 1, 66: 1, 40: 2, 10: 2, 11: 1, 50: 2, 20: 1, 22: 1, 55: 1}

That’s it. You had learnt various approaches in Python programming to find the number of occurrences of each list item in the given list.

Hope it helped 🙂

You’ll also like:

References

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments