Ways to count occurrences of the list items in Python
Tags: python, productivity, computerscience, codenewbie
Published: 2020-08-02 16:47:34

### Table Of Contents
   * [Introduction](#introduction)
   * [Count](#count)
   * [operator](#operator)
   * [Counter](#counter)
   * [Pandas](#pandas)
   * [loop and Dict](#loop)

### Introduction <a name="introduction"></a>
Hey guys,
hope you all are doing well during this crisis. I have been coding in python for a while and came across a situation where I had to count some occurrences for a particular element in the list and I came across a few cool techniques to do this. So, let's get started.

### Count <a name="count"></a>

well, this is the most interesting and easy method to use, let's look at an example

```python
>>> l = ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e']
>>> l.count('a')
5
>>> l.count('e')
1
```

we can also use this method to get a count of all distinct values at once.

```python
>>> l = ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e']

>>> [[x,l.count(x)] for x in set(l)]
[['d', 2], ['b', 4], ['c', 3], ['e', 1], ['a', 5]]

>>> dict((x,l.count(x)) for x in set(l))
{'d': 2, 'b': 4, 'c': 3, 'e': 1, 'a': 5}
```

### operator <a name="operator"></a>

In this method, you have to import the `operator` module. Let's have a quick example

```python
>>> l = ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e']

>>> import operator as op

>>> op.countOf(l,'a')
5
```

let's go to the next method

### Counter <a name="counter"></a>

Here you have to import `Counter` from the `collections` module.

```python
>>> l = ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e']

>>> from collections import Counter

>>> Counter(l)
Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1})
```

This can also be used to get a count for a single element

```python
>>> from collections import Counter

>>> c = Counter(l)

>>> c['a']
5
```

### Pandas <a name="pandas"></a>

pandas is a very popular library and it can also be used for this purpose

```python
>>> import pandas as pd

>>> l = ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e']

>>> my_count = pd.Series(l).value_counts()

>>> my_count
a    5
b    4
c    3
d    2
e    1
dtype: int64
```

in case if you are looking for a count of a particular element

```python
# you should run steps in the previous snippet before running this i.e. importing pandas, etc
>>> my_count['a']
5
```

### loop & Dict <a name="loop"></a>
This is the *fastest* way among all the above, let's go to an example

```python
>>> def countElement(a):
...     g = {}
...     for i in a:
...         if i in g:
...             g[i] +=1
...         else:
...             g[i] =1
...     return g
...
>>> countElement(l)
{'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1}
```

Well, this is it for the post. Hope you enjoyed it and it was helpful to you in some way.

Have a great day,
Happy Learning.

### Other Reads

{% link https://dev.to/kedark/free-illustrations-resources-for-the-web-make-your-next-project-cooler-525c %}

{% link https://dev.to/kedark/three-dots-in-js-spread-and-rest-explained-40e %}

{% link https://dev.to/kedark/stuck-at-home-corona-spend-your-time-learning-for-free-15-000-hours-content-1d29 %}

Don't forget to follow me on [Twitter](https://twitter.com/KodgireKedar) or [Dev](https://dev.to/kedark) for updates on amazing technology stuff.