There are many different ways to count all elements in a list with Python. You could have also different situations:

Python how to count elements of a list:

Python 3 Count elements in ordered list

Below you can see how to find the element frequency in a python ordered list. The output is a dictionary showing the element and it's frequency in the list:

from itertools import groupby

ordered_list = [1,1,1,2,2,2,2,2,3,3,3,4,4,4,5,5]
print({key:len(list(group)) for key, group in groupby(ordered_list)})

result:

{1: 3, 2: 5, 3: 3, 4: 3, 5: 2}

Note that this example will not work correctly if the list is not sorted.

Python 3 Count elements in unordered list

Very often you will need to count the elements of a list with heterogeneous elements. Sometimes the sort operation will be not applicable or not well suited for your example (if you need to keep the order of the elements). In this case you can use count for unordered lists:

unordered_list = [5,4,5,1,1,1,2,2,2,2,2,3,3,3,4,4,4,5,5]
freq = {x:unordered_list.count(x) for x in unordered_list}
print(freq)

result:

{5: 4, 4: 4, 1: 3, 2: 5, 3: 3}

You can use this count for list with relatively small number of items. Because the count(x) is expensive operation. It's better to use: collections.Counter() like:

from collections import Counter
unordered_list = [5,4,5,1,1,1,2,2,2,2,2,3,3,3,4,4,4,5,5]

counter=Counter(unordered_list)

result = [list(counter.keys()),list(counter.values())]
print(result)

result:

[[5, 4, 1, 2, 3], [4, 4, 3, 5, 3]]

Python 3 Count elements with for loop

The old good for loop could be good for small lists and beginners:

unordered_list = [5,4,1,1,1,1,2,2,2,2,3,3,4,5,5]
res_dict = {}
for item in unordered_list:
    res_dict[item] = res_dict.get(item, 0) + 1
print(res_dict)

result:

{5: 3, 4: 2, 1: 4, 2: 4, 3: 2}   

Python 3 Count elements with pandas and numpy

You can extent the usage of collections.Counter() with pandas and numpy in order to get the result into pandas dataframe:

import pandas as pd
import numpy as np
from collections import Counter
unordered_list = [5,4,5,1,1,1,2,2,2,2,2,3,3,3,4,4,4,5,5]

counter = Counter(unordered_list)

result = [list(counter.keys()),list(counter.values())]
print(result)
df = pd.DataFrame(np.array(kk).T, columns=['Letter','Count'])
print(df)

result:

   Letter  Count
0       5      4
1       4      4
2       1      3
3       2      5
4       3      3

Python 3 Count elements in ordered list

If you need to filter the elements for the count you have several options:

  • using list comprehension
  • use simple loop

The simple for loop looks like:

unordered_list = [5,4,1,1,1,1,2,2,2,2,3,3,4,5,5]
res_dict = {}
for item in unordered_list:
    if item % 2 == 0:
        res_dict[item] = res_dict.get(item, 0) + 1
print(res_dict)

result:

{4: 2, 2: 4}

In this way we are counting only the even items from the list. If you want to use list comprensions of Python then you can do:

unordered_list = [5,4,1,1,1,1,2,2,2,2,3,3,4,5,5]
i = sum(1 for i in unordered_list if i % 2 == 0)
print(i)

The result of the code above will be:

6