When working with data, we are often going to need to count items, create dictionaries values before we know keys to store them in, or maintain order in a dictionary.

Counter is a powerful tool for counting, validating, & learning more about the elements within a dataset that is found in the collections module. We pass an iterable (list, set, tuple) or a dictionary to the Counter. We can also use the Counter object similarly to a dictionary with key/value assignment, for example, counter[key] = value.

A common usage for Counter is checking data for consistency prior to using it.

Counter Module

We can see each type from the list & the number of times it was found in the list. We can also see how many restaurants are in the counter by using Restaurant as the index and printing it.

Using Counter to Find the Most Common

Let’s print the top 3 eatery types in the NYC park system with the most_common() method & pass it 3 as the number items to return.

most_common() is excellent for frequency analytics & finding out how often an item occurs.

Interactive Example

  • Use the data from the Chicago Transit Authority on ridership.
  • Import the Counter object from collections.
  • Print the first ten items from the stations list.
  • Create a Counter of the stations list called station_count.
  • Print the station_count.

When we run the above code, it produces the following result:



Data Scientist & Machine Learning Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store