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:06 The first part says merging dictionaries given the coin value dictionary create a new dictionary, new value, that has the coin value values
0:15 and has the following keys and values, so BCH entry and ETH entry, use extended unpacking no dictionary methods or inserts.
0:24 So I've got coin value right here, it wants me to make a new dictionary below it called new value
0:32 and it needs to be a dictionary that has these new keys and also has the original keys.
0:40 So in order to get the original keys without calling a dictionary method or using an insert I can do this extended unpacking here
0:48 and I can just say coin value right there with the ** in front of it, that's going to unpack the dictionary into this other dictionary
0:56 and then I can say BCH is 1650 and ETH is 1055. Let's give this a run see if it works.
1:16 Okay, so I'm on the next one, so it looks like that worked there. So again, this is a nice way to unpack dictionaries into other dictionaries.
1:25 And if you have multiple dictionaries, you can use multiple dictionaries here, it's not limited to just one.
1:31 Okay, the next part is create a set of the keys from new value by unpacking. Put the result in coins.
1:40 So we want the keys of the new value guy in this coins guy, so in order to get the keys of this we can use this extended unpacking operator
1:51 and just say I want * of new value if we treat new value as a sequence, Python treats a dictionary as a sequence of keys. This should give us the keys.
2:04 Let's run it and see if it works. Okay, it says I can't use a starred expression here.
2:16 That's because I can't have a star by itself, I need to put a comma there at the end. Let's run it again.
2:22 Okay, and now I got an error where it said the assertion failed because I've got a tuple that's not equal to a set.
2:28 Apparently I didn't read well enough that said create a set of the keys. So one way to create a set of them is to put this into a set.
2:37 Let's try and run this and see if it works. Okay, the next part says create a list of the keys from the new value
2:48 and the other coins tuple, put the result in all coins. So we want to make a new variable called all coins and it should have the keys from new value
2:59 and it should have the other coins tuple, what's in other coins, so here's new value
3:07 if we want the keys from that, we can just do that star to get the keys from the new value
3:12 and it wants it in the list, so we're going to say * new value, and the other coins tuple. The other coins tuple is defined right here,
3:25 and if we just want to include that in here, we can just say I'm going to start other coins include that as well.
3:31 So again, this is a nice little syntax here. I'm saying in my list, I want to include this sequence here.
3:38 And this is a dictionary, but we're pulling out the keys and we're also including the sequence here unpacking it.
3:45 This happens to be a tuple but we're going to pull out those guys and insert them into our list. Let's run it and make sure it works.
3:55 Okay, so we get no failures here.