#100DaysOfCode in Python Transcripts
Chapter: Days 4-6: Collections module
Lecture: Defaultdicts: factory for data structures

Login or purchase this course to watch this video and the rest of the course contents.
0:00 A second data type I want to show you, today about, is defaultdict. And it's very useful when you're building up a nested data
0:10 structure and you have to account for keys not being there. Now first of all, what's the problem with keys? Let's define a very simple dictionary,
0:21 just one user and role, and let's do a lookup by key. So... Bob is there. Julien is... Oops, not there and that gives you a key error.
0:34 There's a way around it by using users get. Oop, get Bob.. and users get... Julien... which returns none. But how do you deal with that
0:52 when you're building up a collection? Now let's get some data. I'm going to define a list of tuples. A challenge is done. And it has... tuples of name,
1:07 and a number of the challenge that has been completed. Let me type that out. So the goal is to convert this into a dictionary.
1:21 Let me show you what happens if I use a normal dictionary. For name challenge in... challenges done. Challenges dictionary... name append...
1:39 challenge. Oops, it says Mike is not in the dictionary. In the first round, he is indeed not in the dictionary.
1:49 So here is where is you really want to use a defaultdict. So to define one, challenges... Equals defaultdict, and you need to define
2:02 what type the values hold. So in this case, the key is the user and the value is a list of challenge numbers.
2:11 So I put list here and the rest is kind of the same. For name challenge in challenges done. Challenges... Name... append... challenge.
2:28 I'm almost sure this works. So, yes, we have a defaultdict which holds lists and here you see keys are Bob, Julien, and Mike
2:37 and values are list of challenge ids. So you see here, we work around the key error. The defaultdict has the mechanisms to use a factory
2:50 to initialize the data type that the values need to hold and yes, it's safer, it's cleaner, and for this type
2:58 of task, I highly recommend that you use this data type.


Talk Python's Mastodon Michael Kennedy's Mastodon