collections.Counter() in Python

In Python, the Counter class from collections is an efficient way to count how many times an element occurs in a list. It is used to create something like a dictionary whose stored elements are the keys and their counts are the values. This makes frequency analysis much easier, especially with larger datasets, as it avoids manual loops or counting. When the Counter is used, it allows for fast retrieval of the count of any element and also further identifies the most common elements from a list. This is a very common method used in data analysis, text parsing, and many applications that require the counting of elements.

What are collections.Counter in Python

The collections module provides specialized container data types built on top of the regular built-in types. Inside the collections module is a counter, which is used specifically to count the number of times some element occurs in an iterable datatype, most commonly in a list. Using Counter would generate a dictionary like object wherein the elements themselves are the keys and their respective counts are the values. It is also more convenient and efficient for frequency analysis since it does not require writing long manual loops. Regular uses of the counter are data processing, for example, text analysis and statistical analysis, where the goal is to expediently obtain the most common elements in that data.
Syntax:

from collections import Counter
Counter(list)

The collection module contains a counter that counts the number of occurrences of each element in the list and returns it as a dictionary-like object.

Example 1:

Python

Output:

collections.counter (1)

The above code uses the collections module, which contains a counter to count the number of occurrences of each name in the students list. It gives the total count of each name, but declaring this student_count[“rahul”] only gives the count of rahul and stores the result in student_count and prints it.

Example 2:

Python

Output:

collections.counter (2)

The above code uses the collections module, which contains a counter to count the number of occurrences of each course in the courses list. It gives the total count of each course, but declaring this course_count[“intellipaat”] only gives the count of intellipaat and stores the result in course_count and prints it.

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion

The collections.counter function can count multiple elements by occurrences, while the counter class effectively counts unique occurrences of each element in the list by being built using the collections module, which provides us with the Counter class. We place the keys, which are the elements, in a dictionary-like structure, and the value is the frequency. Besides, Counter presents a scenario in which large datasets can be processed efficiently and boasts some useful functions. Other useful functions include the possibility of analyzing text, processing data, and making statistical calculations.

You can learn more about Python in the Python Course and also explore Python Interview Questions prepared by industry experts.

Other Methods to Count the Occurrences in a Python List

  • Python List Count() Method
  • How to Count List Elements with a For Loop in Python
  • Pandas value_counts() in Python Lists
  • Python List countOf() Method

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner