#100DaysOfCode in Python Transcripts
Chapter: Days 4-6: Collections module
Lecture: Defaultdicts: factory for data structures
0:00 A second data type I want to show you,
0:02 today about, is defaultdict.
0:05 And it's very useful when you're building up a nested data
0:09 structure and you have to account for keys not being there.
0:14 Now first of all, what's the problem with keys?
0:16 Let's define a very simple dictionary,
0:20 just one user and role,
0:22 and let's do a lookup by key.
0:26 Bob is there.
0:28 Julien is...
0:30 Oops, not there and that gives you a key error.
0:33 There's a way around it by using users get.
0:38 Oop, get Bob..
0:41 and users get...
0:46 which returns none.
0:49 But how do you deal with that
0:51 when you're building up a collection?
0:53 Now let's get some data.
0:54 I'm going to define a list of tuples.
0:57 A challenge is done.
1:00 And it has...
1:03 tuples of name,
1:06 and a number of the challenge that has been completed.
1:12 Let me type that out.
1:17 So the goal is to convert this into a dictionary.
1:20 Let me show you what happens if I use a normal dictionary.
1:25 For name challenge in...
1:28 challenges done.
1:32 Challenges dictionary...
1:34 name append...
1:40 Oops, it says Mike is not in the dictionary.
1:43 In the first round, he is indeed not in the dictionary.
1:48 So here is where is you really want to use a defaultdict.
1:52 So to define one, challenges...
1:57 Equals defaultdict, and you need to define
2:01 what type the values hold.
2:02 So in this case, the key is the user
2:06 and the value is a list of challenge numbers.
2:10 So I put list here and the rest is kind of the same.
2:13 For name challenge in challenges done.
2:27 I'm almost sure this works.
2:29 So, yes, we have a defaultdict which holds lists
2:33 and here you see keys are Bob, Julien, and Mike
2:36 and values are list of challenge ids.
2:41 So you see here, we work around the key error.
2:44 The defaultdict has the mechanisms to use a factory
2:49 to initialize the data type that the values need to hold
2:52 and yes, it's safer, it's cleaner, and for this type
2:57 of task, I highly recommend that you use this data type.