From the course: Hands-On Advanced Python: Data Engineering Basics
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Count values with defaultdict - Python Tutorial
From the course: Hands-On Advanced Python: Data Engineering Basics
Count values with defaultdict
- [Instructor] Earlier in the course, I used a dictionary to count how many years of weather data we had in the dataset. It's actually pretty fairly common to use dictionaries to keep track of counts and other data values and then map them back to keys. There's a data structure in Python's collections module that makes this process a little bit easier called the defaultdict, and I'm going to be using it in this example. So let's go ahead and open up the defaultdict source code file. And I covered the theory behind this collection class in my Advanced Python: Working With Data course. So if you want to learn a little more about the basics, be sure to check that out here in the library. So in order to use this class, I first have to import it from the collections module that is a part of the Python standard library. So from the collections, I'm going to import defaultdict. Oh, oops, one too many Ls. Here we go. Now…