+1 vote
Is there a function in Python that counts the number of occurrences of an item in Python?
Apr 16, 2018 in Python 35,111 views

## 8 answers to this question.

+1 vote

I think the function you are looking for is count():

```>>> [1,1,2,3,5,6,8,6,3,2,4].count(3)
2```
• 4,260 points
+1 vote
```a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
from itertools import groupby
[len(list(group)) for key, group in groupby(a)]```

Output:

`[4, 4, 2, 1, 2]`
answered Oct 18, 2018 by nabarupa
thank you so much. you saved my day. i was really stuck at finding this answer.
+1 vote
```import collections
a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
counter=collections.Counter(a)
print(counter)
# Counter({1: 4, 2: 4, 3: 2, 5: 2, 4: 1})
print(counter.values())
# [4, 4, 2, 1, 2]
print(counter.keys())
# [1, 2, 3, 4, 5]
print(counter.most_common(3))
# [(1, 4), (2, 4), (3, 2)]```

If you are using Python 2.6 or older, you can download it here.

Hope this helps!!

If you need to know more about Python, join Python certification course today.

Thanks!

answered Oct 18, 2018 by Esha
+1 vote

To count the number of appearances:

```from collections import defaultdict

appearances = defaultdict(int)

for curr in a:
appearances[curr] += 1```

To remove duplicates:

`a = set(a) `
answered Oct 18, 2018 by tinitales

Hi, @Everyone,

There are a few functions that can be used to get the frequency of occurrence in a string. So, how do you count frequency in Python?

using list(): Split the string into a list containing the words by using a split function (i.e. string.split()) in python with delimiter space.

using set(): Split the string into a list containing the words by using a split function (i.e. string.split()) in python with delimiter space.
Use set() method to remove a duplicate and to give a set of unique words

Using a Dictionary

• 65,910 points

Here is one of the ways to get the number of occurrences when you working with array in python. So, I am sharing one example on how do you count the frequency of an element in an array in Python?

Loop through the array and count the occurrence of each element as frequency and store it in another array fr:

1    2   8  3   2   2   2   5   1

```arr = [1, 2, 8, 3, 2, 2, 2, 5, 1];

#Array fr will store frequencies of element

fr = [None] * len(arr);

visited = -1;

for i in range(0, len(arr)):

count = 1;

for j in range(i+1, len(arr)):

if(arr[i] == arr[j]):

count = count + 1;

#To avoid counting same element again

fr[j] = visited;

if(fr[i] != visited):

fr[i] = count;

#Displays the frequency of each element present in array

print("---------------------");

print(" Element | Frequency");

print("---------------------");

for i in range(0, len(fr)):

if(fr[i] != visited):

print("    " + str(arr[i]) + "    |    " + str(fr[i]));

print("---------------------");  ```

Using a dictionary

Here we capture the items as the keys of a dictionary and their frequencies as the values.

```list = ['a','b','a','c','d','c','c']
frequency = {}
for item in list:
if (item in frequency):
frequency[item] += 1
else:
frequency[item] = 1
for key, value in frequency.items():
print("% s -> % d" % (key, value))```

Running the above code gives us the following result −

```a -> 2
b -> 1
c -> 3
d -> 1```

answered Jul 12, 2020 by Swati

Given an unsorted list of some elements(may or may not be integers), Find the frequency of each distinct element in the list using a dictionary.

Example:

```Input : [1, 1, 1, 5, 5, 3, 1, 3, 3, 1,
4, 4, 4, 2, 2, 2, 2]
Output : 1 : 5
2 : 4
3 : 3
4 : 3
5 : 2
Explanation : Here 1 occurs 5 times, 2
occurs 4 times and so on...
```
 # Python program to count the frequency of  # elements in a list using a dictionary    def CountFrequency(my_list):        # Creating an empty dictionary      freq = {}     for item in my_list:         if (item in freq):             freq[item] += 1         else:             freq[item] = 1        for key, value in freq.items():         print ("% d : % d"%(key, value))    # Driver function if __name__ == "__main__":      my_list =[1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2]        CountFrequency(my_list)

Output:

``` 1 :  5
2 :  4
3 :  3
4 :  3
5 :  2
```

Time Complexity:O(N), where N is the length of the list.

• 10,520 points

## How do I obtain the index list in a NumPy Array of all the NaN values present using Python?

Hi, it is pretty simple, to be ...READ MORE

## how do I check the length of an array in a python program?

lets say we have a list mylist = ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

## Lowercase in Python

You can simply the built-in function in ...READ MORE