# Counting the frequencies in a list using dictionary in Python

## Counting the frequencies in a list using dictionary in Python

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...```

The problem can be solved in many ways. A simple approach would be to iterate over the list and use each distinct element of the list as a key of the dictionary and store the corresponding count of that key as values. Below is the Python code for this approach:

 `# 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.

Alternative way: An alternative approach can be to use the list.count() method.

 `# Python program to count the frequency of` `# elements in a list using a dictionary` `def` `CountFrequency(my_list):` `    ` `    ``# Creating an empty dictionary` `    ``freq ``=` `{}` `    ``for` `items ``in` `my_list:` `        ``freq[items] ``=` `my_list.count(items)` `    ` `    ``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(N2), where N is the length of the list. The time complexity list.count() is O(N) alone, and when used inside loop it will become O(N2).

Alternative way:An alternative approach can be to use the dict.get() method. This makes the program much more shorter and makes understand how get method is useful instead of if…else.

 `# Python program to count the frequency of` `# elements in a list using a dictionary` `def` `CountFrequency(my_list):` `    ` `   ``# Creating an empty dictionary` `   ``count ``=` `{}` `   ``for` `i ``in` `[``1``, ``1``, ``1``, ``5``, ``5``, ``3``, ``1``, ``3``, ``3``, ``1` `,``4``, ``4``, ``4``, ``2``, ``2``, ``2``, ``2``]:` `    ``count[i] ``=` `count.get(i, ``0``) ``+` `1` `   ``return` `count` `# Driver function` `if` `__name__ ``=``=` `"__main__"``:` `    ``my_list ``=``[``1``, ``1``, ``1``, ``5``, ``5``, ``3``, ``1``, ``3``, ``3``, ``1``, ``4``, ``4``, ``4``, ``2``, ``2``, ``2``, ``2``]` `    ``print``(CountFrequency(my_list))`
Output:
`{1: 5, 5: 2, 3: 3, 4: 3, 2: 4}`

Last Updated on August 30, 2021 by admin

## Python program to swap two elements in a listPython program to swap two elements in a list

Python program to swap two elements in a list Given a list in Python and

## Python List insert() MethodPython List insert() Method

Python List insert() The Python List insert() method is an inbuilt function in Python that inserts a

## Python list remove()Python list remove()

Python list remove() Python List remove() is an inbuilt function in the Python programming language that removes

## Python List ExercisePython List Exercise

Python List Exercise Basic List Programs Python program to interchange first and last elements in

## f-strings in Pythonf-strings in Python

f-strings in Python PEP 498 introduced a new string formatting mechanism known as Literal String Interpolation or more

## Check if element exists in list in PythonCheck if element exists in list in Python

Check if element exists in list in Python List is an important container in python

## Python CheatsheetPython Cheatsheet

Hello World >>> print("Hello, World!") Hello, World! The famous “Hello World” program in Python

## Numpy size() function | PythonNumpy size() function | Python

Numpy size() function | Python In Python, numpy.size() function count the number of elements along