Write Pythonic Code Like a Seasoned Developer Transcripts
Lecture: Tuple assignment and unpacking
0:01 Tuples play a central role in Python, we are going to look at variety of techniques
0:05 and ways to use tuples that are particularly Pythonic,
0:09 let's start with assignment and unpacking, and we'll start in code.
0:13 So, you probably know that tuples are defined like this, so it could be a number,
0:19 another number, a string even be like an array.
0:23 So if we wanted to just look at the our little creation here, we can go like this,
0:28 we'll see that it's actually not the parenthesis that have anything to do with this,
0:34 usually, we could just as well write like this, and we get the same output,
0:38 it's the commas, not the parenthesis that make the tuple.
0:44 In fact, we can come down here and can have a shorter one, like this,
0:49 sometimes you want to have a tuple with just one element in it, we could write this,
0:53 there is a tuple of length 1, let's find out. There, a tuple of length 1.
1:00 So that tuples, they can't grow after they are created,
1:03 they are basically immutable objects that it can be added to or moved, things like that.
1:08 So that's not the Pythonic thing, that's just tuples.
1:12 We get values out of them like this, if we wanted to say
1:15 print out the word cat that's in the second index position, zero based,
1:18 so there we'd print cat if we didn't actually do this, there that prints cat,
1:28 so the first thing we want to look at is unpacking these into variables,
1:32 let's go over here and work with the shorter version,
1:35 so let's suppose we had the number 7 and the word cat and that was all,
1:38 if I want to have 2 variables, one for the number and one for the let's say animal,
1:45 I could say n for number = this and I could say a for animal = that,
1:52 that would not be Pythonic, instead, what you would say is n, a = t,
1:57 and Python will unpack the first value into the first thing here,
2:01 the second value into the second thing there, and so on.
2:04 Now, if we had another one, like another number here, and we try to run this,
2:08 you'll see it crashes because it says too many values,
2:11 so if we are doing that we would typically say
2:15 I don't care what this value is and use an underscore to say
2:17 please ignore it, so let's run that.
2:19 And PyCharm is just saying look you assign these values
2:22 and you never use them here so let's do this.
2:24 Now, maybe we should show you what numbers came out
2:27 or what values came out of there so we'll say like so, n is 7, a is cat, beautiful.
2:32 And underscore, well we could grab it but we don't care, that's the whole point of it.
2:37 So this concept lets us assign values in a single line
2:40 so I could say x, y= 1, 2 then I could print x and y and I would get 1 and 2.
2:49 Finally, this tuple unpacking is a very important when we are talking about loops,
2:55 remember our numerical for in loop where it gives us both index and the item,
3:00 we wrote something like this, for index, item in enumerate, something like this,
3:06 for enumerating over a collection, we'll get the index and the item,
3:09 so 0 had 1, cat and so on, there, 0 goes to 1, 2 goes to cat,
3:17 well this returns a tuple and we unpack them into index and item,
3:22 so this tuple unpacking is super important
3:25 and we'll see more of it as we go through this chapter.
3:28 So we saw we can create a tuple, we can unpack it into a variety of values,
3:31 should we have a tuple of 4 values we unpack it into greeting and one into enclosing,
3:36 then we just print them out you see the values come out
3:39 as if they had been pulled out individually,
3:41 we also saw that enumerate returns tuples
3:44 and the way that we actually separate the values in a really nice clean way
3:47 maybe didn't even notice this one has happened
3:50 is we are actually using tuple unpacking into those two items
3:53 as it comes back from enumerate.