Dark Mode On/Off

# Counting the frequencies in a list using dictionary in Python

In this tutorial, you will learn to write a program in Python that will count the frequency of elements in a list using a dictionary. For a given list with multiple elements, we have to count the occurrence of each element in the list and display it as output. This can be done by creating a dictionary and using some built-in methods which will be discussed below.

Look at the input-output format to understand better.

Input: ['a', 'a', 'a', 'b','b']

Output: {'a': 3, 'b': 2}

To solve this problem we can follow these approaches-

1. by iterating over the list and counting frequency
2. using list.count() method
3. using dict.get() method

## Approach 1: An iterative approach

We will define a function that accepts the list as a parameter. Then we will create a dictionary where the list element is the key and its frequency will be the value. To get frequency we will traverse the list and check if the element is already present in the dictionary or not and keep a count accordingly.

### Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function that will count the frequency of elements in the list

Step 2- Create an empty dictionary

Step 3- Run a loop to traverse the list items

Step 4- To count the frequency, check if the element exists in the dictionary

Step 5- If yes, then increase the value present at the element key by 1,

Step 6- else add that element as key and set its value as 1

Step 7- Print the dictionary as the result

### Python Program 1

Look at the program to understand the implementation of the above-mentioned approach. To check if the element of the list is already present in the dictionary or not we have used if conditional statement along with the in operator.

``````def CountFreq(li):
freq = {}
for item in li:
if (item in freq):
freq[item] += 1
else:
freq[item] = 1
print(freq)
li =[1, 1, 3, 2, 6, 5, 3, 1, 3, 3, 1, 4, 6, 4, 4, 2, 2, 2, 2]
CountFreq(li)
``````

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

## Approach 2: using list.count()

The `list.count()` method returns the total count of an element that is passed in it. Using this method we will store the count as value and the character as the key in the dictionary.

### Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function that will count the frequency of elements in the list

Step 2- Create an empty dictionary

Step 3- Run a loop to traverse the list items

Step 4- Use count to get the frequency of the elements and store them in the dictionary

Step 5- Print the dictionary as the result

Step 6- Declare a list and pass the list in the function

### Python Program 2

Look at the program to understand the implementation of the above-mentioned approach. Using a for loop we will store the value returned by the` count()` method in the dictionary.

``````def CountFreq(li):
freq = {}
for items in li:
freq[items] = li.count(items)
print(freq)
li =['a', 's', 'a', 's', 'c', 'c', 'c','b']
CountFreq(li)``````

{'a': 2, 's': 2, 'c': 3, 'b': 1}

## Approach 1: using dict.get()

The `dict.get()` method of the dictionary class returns the value of the specified key if the keys if from the dictionary. We can use this method instead of using the if-else condition which is discussed in the first approach, to get the frequency of list items.

### Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function that will count the frequency of elements in the list

Step 2- Create an empty dictionary

Step 3- Run a loop to traverse the list items

Step 4- Use get() and pass the list items in it to get the frequency

Step 5- Print the dictionary as the result

Step 6- Declare a list and pass the list in the function

### Python Program 2

Look at the program to understand the implementation of the above-mentioned approach. We have created an empty dictionary and stored the list elements as keys and their frequency as values.

``````def CountFreq(li):
freq = {}
for items in li:
freq[items] = freq.get(items,0)+1
print(freq)
li =['a', 'a', 1, 1, 1, 'b', 'b', 'b', 2, 2, 2]
CountFreq(li)``````

{'a': 2, 1: 3, 'b': 3, 2: 3}

## Conclusion

In this tutorial, we have seen three different approaches for calculating and finding the frequency of elements of a list using a dictionary in Python. We have used an iterative approach, the `list.count()` method and the `dict.get()` method to solve this problem.