Counting With Python's Counter (Overview)
Counting several repeated objects at once is a common problem in programming. Python offers a bunch of tools and techniques you can use to approach this problem. However, Python’s Counter
from collections
provides a clean, efficient, and Pythonic solution.
This dictionary subclass provides efficient counting capabilities out of the box. Understanding Counter
and how to use it efficiently is a convenient skill to have as a Python developer.
In this video course, you’ll learn how to:
- Count several repeated objects at once
- Create counters with Python’s
Counter
- Retrieve the most common objects in a counter
- Update object counts
- Use
Counter
to facilitate further computations - Implement
Counter
instances as multisets
00:00
Welcome to Counting With Python’s Counter
. My name is Christopher, and I will be your guide. This course is all about Python’s Counter
class in the collections
library.
00:10
You’ll learn about counting problems in computing, how to use the Counter
class to write less code, practical algorithms where the Counter
class is helpful, and multisets.
00:24
Sample code in this course was tested with Python 3.10. The Counter
class is mostly unchanged. I do use f-strings in a couple of places, but besides that, the information is fairly version-agnostic.
00:37
A common problem in computing is determining the frequency of items in a sequence, or to put it more simply, counting things or groups of things. If you’re counting one thing, you use a variable, but if you’re trying to count a whole bunch of things or track multiple things, then Python’s Counter
class can help you do that.
Become a Member to join the conversation.