Hi Reader!
My goal with Tuesday Tips is to help you get better at data science every week.
I look forward to hearing from you! 💬
Find past tips at tuesday.tips. (Yes, that’s a real URL!)
When I’m working with data in Python, I tend to use either built-in types (like lists and dictionaries) or third-party types (like NumPy arrays and pandas DataFrames), depending on the size and complexity of the data.
But occasionally, there are scenarios in which I use a more specialized type, such as “Counter” from the collections module. It solves a specific problem so well that creating my own solution would be wasted effort!
For example, let’s say that I need to count how many times a specific number appears in a dataset. Counter is the perfect tool for solving such a problem!
To start, we would import the Counter class from the collections module (which is part of Python’s standard library):
The basic usage of Counter is that you pass it an “iterable” and it counts the number of times each element appears.
Side note: An “iterable” is anything you can iterate through. If you can loop through an object using a “for loop”, you know it’s an iterable. Examples of iterables include strings, lists, tuples, dictionaries, and files.
For example, let’s pass the string ‘HELLO’ to Counter:
We get back a Counter object, which tells us that the ‘L’ appeared twice and the other letters each appeared once.
Let’s try creating a Counter object from a list of integers:
It counted how many times each integer appears in the list.
Let’s save it and check the type:
This confirms that c has the type Counter (or more formally, “c is an instance of the Counter class”). However, it looks very similar to a dictionary since it’s actually a “subclass” of dict.
As such, it acts like a dictionary, meaning when you pass it a key, it returns a value:
This tells us that the integer 7 appeared 4 times.
If you want the Counter to count more things, you pass another iterable to its update() method:
7 has now appeared 5 times, and 6 has appeared 2 times.
If you want to list the elements from most common to least common, you use the most_common() method, which returns a list of tuples:
7 is the most common, followed by 1, then 6, then 3.
Finally, you can pass this list to the sorted() function to sort it by the first element in each tuple, which is how we might prefer to see the results:
To see more examples and use cases, I recommend checking out the Counter class in Python’s official documentation.
If you enjoyed this week’s tip, please forward it to a friend! Takes only a few seconds, and it truly helps me out! 🙌
See you next Tuesday!
- Kevin
P.S. This is your Machine Learning system?
Did someone awesome forward you this email? Sign up here to receive data science tips every week!
Join 25,000+ intelligent readers and receive AI tips every Tuesday!
Hi Reader, This week, I've got a short tip about AI agents, followed by some Data School news... 👉 Tip #56: What are AI agents? Google is calling 2025 "the agentic era," DeepLearning.AI says "the agentic era is upon us," and NVIDIA's founder says "one of the most important things happening in the world of enterprise is agentic AI." Clearly AI agents are a big deal, but what exactly are they? Simply put, an AI agent is an application that uses a Large Language Model (LLM) to control its...
Hi Reader, Last week, I launched a brand new course: Build an AI chatbot with Python. 120+ people enrolled, and a few have already completed the course! 👏 Want to join us for $9? 👉 Tip #55: Should you still learn to code in 2025? You’ve probably heard that Large Language Models (LLMs) are excellent at writing code: They are competitive with the best human coders. They can create a full web application from a single prompt. LLM-powered tools like Cursor and Copilot can autocomplete or even...
Hi Reader, The Python 14-Day Challenge starts tomorrow! Hope to see you there 🤞 👉 Tuesday Tip: My top 5 sources for keeping up with AI I'll state the obvious: AI is moving incredibly FAST 💨 Here are the best sources I follow to keep up with the most important developments in Artificial Intelligence: The Neuron (daily newsletter) My top recommendation for a general audience. It’s fun, informative, and well-written. It includes links to the latest AI news and tools, but the real goldmine is...