Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Tuples
Lecture: Prefer named tuples

Login or purchase this course to watch this video and the rest of the course contents.
0:01 We've seen how central tuples are to many parts of Python, let's look at a better version of tuples.
0:08 So over here I have a very simple program; we have two methods, "main", which is going to print out some data
0:14 and it's going to get that data from this get_data_tricky version, you probably wouldn't call your function tricky but you know,
0:21 just to highlight it for the course, right? So you can see it returns a list of tuples, here we have some incrementing numbers,
0:28 so maybe this is an id, we have 3 values here in the middle, so those must represent something important, we'll see, not totally sure what that is.
0:39 So we'll come over here, here I've got a little template for printing out some stuff and notice I've got some values just so it can run,
0:47 so here we've got 3 things back and I just put "ones" everywhere. Suppose I want to print out the id, the rating and the position.
0:54 Now remember, this is not the function I am getting it from, so maybe you don't have it handy easy to look at,
1:00 so we are going to come over here and say well, I think that I remember the first one being this and let's see the rating maybe that was next,
1:09 and the last two got maybe those represent the position and we know that doesn't look right because these are like floating point
1:15 and that's like and integer, so maybe actually this is a 3, what was that, that was 1,
1:20 yeah, yeah, OK, so that's right, so this is what we wanted, 0, 3, 2, 1, what if I wanted to add an item, a thing, another element to this tuple?
1:31 How easy this is going to be to maintain, to review, to bring on new people, and so on,
1:38 this is not a good way to work, so let's talk about a better kind of tuple. so let's import collections, and let's define something called a Rating.
1:48 Now this is like defining a class or something like that, like a custom type,
1:53 but we can do it in a very concise short way, using "collections.namedtuple"s. So what you put here is the name, the type name,
2:02 and then you can put the fields separated by commas, so maybe we want to call this id, rating, "x" and "y", something like that.
2:10 So now, give us some space, so PEP 8 is not mad about our spacing, we can write a better version here, instead of doing all this, let's have a rating,
2:24 just like the way you initialize a class, or something like that, here we are going to initialize our rating and it takes an id
2:31 and a rating and then "x" and then "y". Actually, it looks like I have that wrong, so let's put it like this,
2:36 or I would have to reorder my data but let's say it goes like this. Now, let's comment out this tricky version, here we go,
2:42 the data from the better and we'll hide this, it didn't even matter what order it comes in, we don't care, we don't have to look at it,
2:48 so we are going to say now we are going to work with this better version of data,
2:51 I want to put the id first, so let's see what is that, "d." all right, "id". Cool, and we are going to have "d.rating" I think was next, "d.x", "d.y",
3:03 see how much nicer that is? And literally, that is all it takes, name the type, state the basically the names of the positions,
3:12 and then when you allocate it, instead of just saying regular tuple like this, you just allocate it as an instance of a class.
3:19 So let's run it, make sure I didn't pull any sneaky tricks, that it still works, boom, id 1, 2, 3, rating looks right, position x y, great.
3:28 And, these named tuples, they have all the properties that you would expect, so for example, I can come down here I could say
3:37 let's say we want the x y value so I could have, so here, like this, I could say I want to unpack this tuple and I want to print x and y
3:48 so you should just see just the numbers next to each other alongside the other values. There is the x y values we are unpacking.
3:55 So these are regular tuples, they do everything regular tuples do but they are upgraded and have names, wonderful.
4:02 So to create one, we just import the collections module, we create a collections.namedtuple, we give it a name,
4:08 we catch the instance of the class, generate it from name tuple here, and when I use that every time when I allocate one of them.
4:17 We name the positions, this case we have temperature, latitude, longitude and quality, then you can see we create one and we can access it
4:24 in the traditional style in bracket 0 but much better "m.temp", "m.quality". We print it out, we even get a nicer looking string friendly version
4:33 rather than just the basic tuple without the names or understanding of what the positions mean, so named tuples, very Pythonic,
4:42 definitely make use of them.


Talk Python's Mastodon Michael Kennedy's Mastodon