Write a Python program to get the frequency of the elements in a list


This program uses the collections module in Python to count the frequency of elements in a list.

  • The list l is defined and contains a set of numbers.
  • The original list is printed using the print function.
  • The collections.Counter(l) function is used to count the frequency of elements in the list l.
  • The frequency of elements is stored in the variable f.
  • The frequency of elements is printed using the print function.

The collections.Counter function returns a dictionary where the keys are the elements in the list and the values are their respective frequencies. In this way, this program is counting the frequency of each element in the list.

Source Code

import collections
l = [10,30,50,10,20,60,20,60,40,40,50,50,30]
print("Original List : ",l)
f = collections.Counter(l)
print("Frequency of the Elements: ",f)
 

Output

Original List :  [10, 30, 50, 10, 20, 60, 20, 60, 40, 40, 50, 50, 30]
Frequency of the Elements:  Counter({50: 3, 10: 2, 30: 2, 20: 2, 60: 2, 40: 2})

Example Programs