Count occurrences of an item in a List in Python
In this post, we will explore different ways to count the occurrences of an item in a Python list.
1. Using list.count() function
One of the simplest method to get the occurrences of an item in a list is to use the list.count() function. This function is a built-in function of a list object that returns the number of times an item appears in a list.
|
1 2 3 4 5 |
ints = [1, 2, 3, 4, 2, 1] item = 2 freq = ints.count(item) print(freq) # 2 |
2. Using operator.countOf() function
Another way to count the occurrences of an item in a Python list is to use the operator.countOf() function from the operator module. This function returns the number of occurrences of a value in an iterable. It is similar to the list.count() function but it can be used with other types of iterables as well.
|
1 2 3 4 5 6 7 8 |
import operator ints = [1, 2, 3, 4, 2, 1] item = 2 freq = operator.countOf(ints, item) print(freq) # 2 |
3. Using collections.Counter() function
If we need the count of multiple items from the list, we can use the collections.Counter() function from the collections module. This function returns a dictionary-like object that contains counts of unique items in an iterable. The keys are the items and the values are their counts. We can then access the count for a specific item by passing the key to the get() function. Here is an example of how to use this function:
|
1 2 3 4 5 6 7 |
from collections import Counter ints = [1, 2, 3, 4, 5] item = 3 freq = Counter(ints).get(item) print(freq) # 1 |
4. Using dictionary
Alternatively, we can create a frequency map using a dictionary to get the total number of occurrences of each item in a list. The following example demonstrates this by using the set() function to remove the duplicates and then use the count() function to get the frequency of each item:
|
1 2 3 4 5 |
ints = [1, 2, 3, 4, 5, 3] item = 3 freq = {i: ints.count(i) for i in set(ints)} print(freq.get(item)) # 2 |
However, this approach is not recommended for large lists. The time complexity of it is quadratic since the count() function is called once for each distinct element in the list. We can improve the performance by creating the frequency map manually, as shown below:
|
1 2 3 4 5 6 7 8 |
ints = [1, 2, 3, 4, 5, 3] item = 3 freq = {} for i in ints: freq[i] = freq.setdefault(i, 0) + 1 print(freq.get(item)) # 2 |
5. Using pandas.Series.value_counts() function
We can also use the pandas.Series.value_counts() function to count the occurrences of an item in a list. This function returns a Series object that contains the counts of each unique value in the original list. Here is a simple example of its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import pandas as pd ints = [1, 2, 3, 4, 2, 1] item = 2 # convert the list to a pandas Series series = pd.Series(ints) # count the occurrences of each value in the Series print(series.value_counts()) ''' Output: 1 2 2 2 3 1 4 1 Name: count, dtype: int64 ''' |
That’s all about counting occurrences of an item in a list in Python.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)