From the course: Advanced Python: Working With Data

Unlock the full course today

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

Default dictionaries

Default dictionaries

- [Instructor] Just about every non-trivial Python program uses dictionaries in some way. They are a great way to associate data values with unique keys for fast lookup. The collections module provides two interesting dictionary subclasses to help out with some common scenarios where a regular dictionary would need unnecessary code. And one such example is the default dict which we'll examine in this example. It's a fairly common scenario to use dictionaries to keep track of data such as the result of counting operations. And so here in my code you can see I have a list of different kinds of fruit and I want to be able to count the number of each kind of fruit in the list. So I have some code, right, that creates a dictionary and then iterates over the list and uses the dictionary to keep track of the count of each type of fruit. Now, the code that I have right now is going to produce an error when I try to run it.…

Contents