From the course: Advanced Python: Working With Data

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Counters

Counters

- [Instructor] As I mentioned earlier, one of the common uses of dictionaries is to keep track of the count of individual items. The collections module supplies a counter class which is basically a dictionary subclass for counting hashable objects. Now, you might be saying, well, wait a minute. We saw how to do this earlier with the default dict class, right? And that's true but counters have some nice additional features for working with numbers of items. So let's open up the counter code here in our start folder in chapter two, and we have two lists of student names for two different classes. Now, counters can be initialized in a variety of ways, and in this case I'll create two of them using the name lists, right? So I'm going to write C1 and I'm going to create a new counter, and I'm going to initialize it with the values from class one. And then I'll do the same thing with C2 and that's going to be a counter for…

Contents