Python Program to Count the Frequency of Elements in a List

In this tutorial, we will discuss a Python program to count the frequency of elements in a list.

Related: Python Program to Find Union of Two Lists

Program code to find the union of two lists in Python

# Count the Frequency of Elements in a List in Python
from collections import Counter

def count_frequency(lst):
    return dict(Counter(lst))

input_list = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
frequency = count_frequency(input_list)
print("Frequency of elements in the list:", frequency)

Explanation

  1. Function Definition: The count_frequency function takes a list as input and returns a dictionary containing the frequency of each element in the list.
  2. Main Program: The program defines a list and counts the frequency of each element using the count_frequency function. It then prints the result.

Output

Python Program to Count the Frequency of Elements in a List

  • When you run the above program, it will count the frequency of each element in the list and print the result.

Conclusion

  • In this tutorial, we learned how to count the frequency of elements in a list using a Python program.
  • Understanding this concept is essential for data analysis and enhancing your programming skills.

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *