From the course: Advanced Python

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Counters

Counters - Python Tutorial

From the course: Advanced Python

Start my 1-month free trial

Counters

- [Instructor] As I mentioned earlier, one of the common uses of dictionaries is to keep track of the count of individual items. And the collections modules supplies a counter class which is a dictionary subclass for counting hashable objects. Now you might be saying wait a minute, we saw how to do this earlier with the default dict class. And that's true, but counters have some nice additional features for working with numbers of items. So let's start with an example where we have two lists. I'm gonna open up my counter underscore start example file in my collections folder. And you can see I have two lists of student names for two different classes. So counters can be initialized in a variety of ways. And in this case I'll create two of them, using the name lists. So I'll have C one equals counter for class one, and C two I'll make a counter for class two. And by the way you can see up here on line three I've already imported the counter class from the collections module. So let's…

Contents