Python 3, an illustrated tour Transcripts
Chapter: Language syntax
Lecture: Walk-through: Unpacking Generalizations
0:00 In this video, we're going to look at gen unpack test, so open that up in your editor.
0:05 The first part says merging dictionaries given the coin value dictionary
0:10 create a new dictionary, new value, that has the coin value values
0:14 and has the following keys and values, so BCH entry and ETH entry,
0:18 use extended unpacking no dictionary methods or inserts.
0:23 So I've got coin value right here,
0:25 it wants me to make a new dictionary below it called new value
0:31 and it needs to be a dictionary that has these new keys and also has the original keys.
0:39 So in order to get the original keys without calling a dictionary method or using an insert
0:44 I can do this extended unpacking here
0:47 and I can just say coin value right there with the ** in front of it,
0:51 that's going to unpack the dictionary into this other dictionary
0:55 and then I can say BCH is 1650 and ETH is 1055.
1:08 Let's give this a run see if it works.
1:15 Okay, so I'm on the next one, so it looks like that worked there.
1:19 So again, this is a nice way to unpack dictionaries into other dictionaries.
1:24 And if you have multiple dictionaries, you can use multiple dictionaries here,
1:28 it's not limited to just one.
1:30 Okay, the next part is create a set of the keys from new value by unpacking.
1:35 Put the result in coins.
1:39 So we want the keys of the new value guy in this coins guy,
1:44 so in order to get the keys of this we can use this extended unpacking operator
1:50 and just say I want * of new value
1:54 if we treat new value as a sequence, Python treats a dictionary as a sequence of keys.
2:00 This should give us the keys.
2:03 Let's run it and see if it works.
2:12 Okay, it says I can't use a starred expression here.
2:15 That's because I can't have a star by itself, I need to put a comma there at the end.
2:18 Let's run it again.
2:21 Okay, and now I got an error where it said the assertion failed
2:24 because I've got a tuple that's not equal to a set.
2:27 Apparently I didn't read well enough that said create a set of the keys.
2:31 So one way to create a set of them is to put this into a set.
2:36 Let's try and run this and see if it works.
2:42 Okay, the next part says create a list of the keys from the new value
2:47 and the other coins tuple, put the result in all coins.
2:51 So we want to make a new variable called all coins
2:54 and it should have the keys from new value
2:58 and it should have the other coins tuple, what's in other coins, so here's new value
3:06 if we want the keys from that, we can just do that star to get the keys from the new value
3:11 and it wants it in the list, so we're going to say * new value,
3:20 and the other coins tuple.
3:22 The other coins tuple is defined right here,
3:24 and if we just want to include that in here, we can just say
3:27 I'm going to start other coins include that as well.
3:30 So again, this is a nice little syntax here.
3:33 I'm saying in my list, I want to include this sequence here.
3:37 And this is a dictionary, but we're pulling out the keys
3:40 and we're also including the sequence here unpacking it.
3:44 This happens to be a tuple but we're going to pull out those guys
3:46 and insert them into our list.
3:48 Let's run it and make sure it works.
3:54 Okay, so we get no failures here.