1 00:00:08,060 --> 00:00:13,590 Welcome back. Last time you saw that on the right side of an assignment statement, 2 00:00:13,590 --> 00:00:18,900 multiple expressions separated by commas will be automatically packed into a tuple. 3 00:00:18,900 --> 00:00:24,405 So, line one the variable Julia gets bound to a tuple, 4 00:00:24,405 --> 00:00:27,759 square bracket zero will be Julia, 5 00:00:27,759 --> 00:00:30,860 square bracket one will be Roberts and so on. 6 00:00:30,860 --> 00:00:33,890 But, instead of referring to the elements of 7 00:00:33,890 --> 00:00:37,375 that tuple with square brackets zero and square bracket as one, 8 00:00:37,375 --> 00:00:43,380 we can unpack them all at once into a bunch of different variable names. 9 00:00:43,520 --> 00:00:46,370 So, we've put a bunch of variable names here on 10 00:00:46,370 --> 00:00:50,225 the left side and what that does it's as if we've said, 11 00:00:50,225 --> 00:00:57,575 name equals Julia square bracket zero, 12 00:00:57,575 --> 00:01:05,780 surname equals Julia square brackets 13 00:01:05,780 --> 00:01:09,170 one, and so on. 14 00:01:09,170 --> 00:01:11,840 So, it's just going to take Julia, 15 00:01:11,840 --> 00:01:17,495 which is a tuple and take all of the values from that and put the first value, 16 00:01:17,495 --> 00:01:23,324 it does goes positionally just like we do when we're calling functions, 17 00:01:23,324 --> 00:01:27,970 the first value goes to the first variable name, 18 00:01:27,970 --> 00:01:31,850 the second value goes to the second variable name, and so on. 19 00:01:31,850 --> 00:01:35,840 Notice however that the number of variable names on the left-hand side of 20 00:01:35,840 --> 00:01:37,580 the assignment statement has 21 00:01:37,580 --> 00:01:41,495 the equal the number of values that are on the right hand side. 22 00:01:41,495 --> 00:01:43,730 If I run a, b, c, d, 23 00:01:43,730 --> 00:01:45,095 equals one, two, three, 24 00:01:45,095 --> 00:01:47,340 I will get an error. 25 00:01:49,240 --> 00:01:56,910 We need more than three values to unpack on line one because we have four variable names. 26 00:01:58,600 --> 00:02:01,580 So, if I have four variable names, 27 00:02:01,580 --> 00:02:04,890 that'll be better, it works. 28 00:02:05,770 --> 00:02:08,800 But, if I have five variable names, 29 00:02:08,800 --> 00:02:10,415 I also get an error. 30 00:02:10,415 --> 00:02:14,495 Too many values, not enough variable names. 31 00:02:14,495 --> 00:02:18,380 There's even a way to pass a tuple to a function and have 32 00:02:18,380 --> 00:02:22,980 them automatically unpacked into the parameter names. 33 00:02:23,690 --> 00:02:31,040 In this code, we've defined a function add that just returns the sum of its two inputs. 34 00:02:31,040 --> 00:02:35,315 If I say to add three and four, 35 00:02:35,315 --> 00:02:39,420 that will give me seven. 36 00:02:41,240 --> 00:02:45,845 Now, I can take five and four and I can tell 37 00:02:45,845 --> 00:02:52,075 the add function to get your values from the tuple z. 38 00:02:52,075 --> 00:02:56,150 On line six I'm saying to add the contents of 39 00:02:56,150 --> 00:03:01,220 the tuples z and it unpacks the first element of z into the variable x. 40 00:03:01,220 --> 00:03:02,705 So, x gets five, 41 00:03:02,705 --> 00:03:05,890 y gets four, and we see that nine prints out. 42 00:03:05,890 --> 00:03:09,675 Now, I had to do a special little notation, 43 00:03:09,675 --> 00:03:17,840 this asterisk in order to tell the Python interpreter that I wanted z to 44 00:03:17,840 --> 00:03:21,140 be treated as a tuple whose components would be 45 00:03:21,140 --> 00:03:26,360 unpacked and assigned to the two parameter names x and y. 46 00:03:26,360 --> 00:03:32,570 If instead I tried to just say add of z, 47 00:03:32,570 --> 00:03:34,310 I'm going to get an error 48 00:03:34,310 --> 00:03:40,505 because what it's going to try to do is treat z as a single value, 49 00:03:40,505 --> 00:03:43,985 assign that value to x, 50 00:03:43,985 --> 00:03:49,250 and then it looks for what value should I give to y and there's nothing for y. 51 00:03:49,250 --> 00:03:52,020 So, I get an error here. 52 00:03:52,730 --> 00:03:56,775 Add takes exactly two arguments. 53 00:03:56,775 --> 00:04:00,690 It has two parameter names x and y. 54 00:04:02,750 --> 00:04:06,780 That's what the error message is telling us, 55 00:04:06,780 --> 00:04:08,600 but only one was given. 56 00:04:08,600 --> 00:04:11,390 We only gave it a single value, 57 00:04:11,390 --> 00:04:15,960 the tuple five, four that didn't work. 58 00:04:15,960 --> 00:04:21,470 There was no problem on line six though because the star z said, "Hey, 59 00:04:21,470 --> 00:04:23,750 even though it's a single value, 60 00:04:23,750 --> 00:04:24,890 a tuple five, four, 61 00:04:24,890 --> 00:04:28,040 you should unpack that value and five should go for 62 00:04:28,040 --> 00:04:32,360 the first variable name and four should go for the second one." 63 00:04:32,860 --> 00:04:38,815 Unpacking is particularly useful for making iteration code more readable. 64 00:04:38,815 --> 00:04:43,440 For example, when you iterate over a list of tuples. 65 00:04:43,440 --> 00:04:48,674 In this code, we iterate over the key value pairs in a dictionary. 66 00:04:48,674 --> 00:04:54,550 So, we ask for all of the items from D. So, 67 00:04:54,550 --> 00:05:03,610 that's going to give us a tuple with k1 and three, 68 00:05:03,610 --> 00:05:10,695 and then another tuple with k2 and seven, and so on. 69 00:05:10,695 --> 00:05:15,620 We get a sequence of all of these and we're iterating through that sequence. 70 00:05:15,620 --> 00:05:23,560 So, in line four the value of p will be one of these tuples. 71 00:05:23,560 --> 00:05:30,080 We can ask for p square bracket zero to get k1 and 72 00:05:30,080 --> 00:05:36,620 p square bracket one to get three. 73 00:05:36,620 --> 00:05:39,300 Let's see what happens when that runs. 74 00:05:41,150 --> 00:05:49,315 We have this key k1 value three that's coming from the first tuple. 75 00:05:49,315 --> 00:05:52,480 Remember, we have this format string where 76 00:05:52,480 --> 00:05:55,090 we're substituting in p square bracket zero there, 77 00:05:55,090 --> 00:06:00,555 so we get key colon and then we're substituting in k1, 78 00:06:00,555 --> 00:06:04,520 and then just the word value colon and we substitute in 79 00:06:04,520 --> 00:06:08,465 whatever p square bracket one is at this point in the string, 80 00:06:08,465 --> 00:06:10,310 so we get the three. 81 00:06:10,310 --> 00:06:12,365 On the next iteration, 82 00:06:12,365 --> 00:06:19,770 p is bound to k2 and seven and so we get key colon k2 value colon seven. 83 00:06:21,430 --> 00:06:25,760 Reading that code is a little hard because you have to 84 00:06:25,760 --> 00:06:30,845 remember that when you're using this p square bracket zero, 85 00:06:30,845 --> 00:06:32,765 that's a p was a tuple, 86 00:06:32,765 --> 00:06:37,305 that tuple was representing a key value pair in the dictionary. 87 00:06:37,305 --> 00:06:40,985 Therefore, a p square bracket zero must be the key. 88 00:06:40,985 --> 00:06:47,470 We can use mnemonic variable names to help us make that a little bit more readable. 89 00:06:47,470 --> 00:06:49,420 Let's do this in two steps. 90 00:06:49,420 --> 00:06:58,250 Suppose I say k equals p square bracket zero and v equals p square bracket one, 91 00:07:00,090 --> 00:07:07,225 then I can k and v 92 00:07:07,225 --> 00:07:11,350 here and this is just if I pick a good variable name 93 00:07:11,350 --> 00:07:16,045 like k for representing a key and v for representing a value, 94 00:07:16,045 --> 00:07:19,510 it's easier to read lines six and remember that what I'm 95 00:07:19,510 --> 00:07:23,900 printing out is the key and the value. 96 00:07:23,940 --> 00:07:25,960 Once we have that idea, 97 00:07:25,960 --> 00:07:32,590 we can go even further and instead of unpacking ourselves on line four and five, 98 00:07:32,590 --> 00:07:36,740 we can have Python do it for us. 99 00:07:36,920 --> 00:07:41,440 So, we can stick to variable names here k comma v, 100 00:07:41,440 --> 00:07:45,610 and it's going to automatically take each item and unpack it into 101 00:07:45,610 --> 00:07:50,270 the two variables k and v. This is going to give me exactly the same result. 102 00:07:52,010 --> 00:07:54,295 So, that's unpacking. 103 00:07:54,295 --> 00:07:58,435 You've got one tuple and multiple variable names. 104 00:07:58,435 --> 00:08:03,545 The tuple has to have the same number of values as the number of variable names. 105 00:08:03,545 --> 00:08:08,390 We can do that with explicit assignment where there's 106 00:08:08,390 --> 00:08:13,370 an equal sign or we can do it when there's behind the scenes assignment, 107 00:08:13,370 --> 00:08:17,750 like iterator variables in a for loop as you've seen here, 108 00:08:17,750 --> 00:08:22,040 k and v are having behind the scenes assignment to 109 00:08:22,040 --> 00:08:26,330 a tuple and we're getting them to get unpacked into the two variable names 110 00:08:26,330 --> 00:08:31,310 k and v. There's also behind the scenes assignment whenever you have 111 00:08:31,310 --> 00:08:36,605 parameter names in a function and we can also do the automatic unpacking there, 112 00:08:36,605 --> 00:08:40,130 but that requires you to use the star notation to tell 113 00:08:40,130 --> 00:08:44,730 Python to do the unpacking. I'll see you next time.