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
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:
- Convert list of lists to flat list – flatten list in Python ?
- Concatenate or join two lists using Python
- Single digit number to double digits string conversion in Python
- String Immutability with Examples in Python
- Python Program to Check Given Number is Odd or Even
- Increase the cell width of the Jupyter Notebook in browser
- Add python3 kernel to jupyter IPython notebook ?
- Programs to Print Patterns – Pyramid, Triangle using Star
- How to change the default theme in Jupyter Notebook ?
- Change the Jupyter Notebook startup folder in Windows & Mac
- Get number of elements in a list – Python ?
- Count number of occurrences of a character in string
- Difference between Python’s list methods append and extend ?
- Python equivalent for && (logical and) to use in if statement
- Split list in to chunks of size n in Python
- How to check if the given list is empty in Python ?
- Remove non-numeric characters from string in Python
- Install Python 3 on Windows 10 machine
- Convert negative to positive number in Python
- Create list of lists in Python
- Extract numbers from a string in python
- How to access index and value in for loop – Python?
- Find index of an element in a list – Python
- Prevent the list that changes unexpected after assignment – Python ?