Locked lesson.
About this lesson
What is a tuple and how is it different from a list?
Exercise files
Download this lesson’s related exercise files.
Tuples.docx60.3 KB Tuples - Solution.docx
58.9 KB
Quick reference
Tuples
In this lesson we'll look at tuples, which are very similar to lists.
When to use
Use a tuple when you have data that won't ever change.
Instructions
Create a tuple just like you would a list, but instead of [ ] brackets, use ( ) parenthesis.
my_tuple = ("John", "Tim", "Mary)
To call an item from a tuple:
print(my_tuple[1])
Hints & tips
- Tuples are just like lists, only you can't change them after they have been created
- Tuples may be slightly faster, performance wise, than a list
- 00:04 In this video I want to look at tuples and a tuple is almost exactly like a list,
- 00:09 there's just one subtle difference.
- 00:11 With a list, you can add things and remove things and change things and
- 00:15 do all the things, with a tuple you cannot.
- 00:17 Tuples are what we call immutable.
- 00:19 You can't add things to them, you can't remove them and
- 00:22 you can't do any of those type of things.
- 00:24 Everything else applies as a list so you can add strings, you can add numbers,
- 00:29 you can add other lists, other tuples, other data types to your tuple.
- 00:33 That all stays the same but you can only do it once when you create it,
- 00:36 after that you can't add or remove anything.
- 00:39 So to create a tuple it's just like a list, but
- 00:41 instead of using these square brackets, we use parentheses.
- 00:45 So, now this becomes a tuple.
- 00:48 And let's go ahead and rename this, so I'm going to call this tuple_1.
- 00:51 So if we come down here, and and we go tuple_1,
- 00:54 we save this we can print it out and
- 00:56 you can see it has the parentheses instead of the brackets because it it's a tuple.
- 01:01 Same thing, has John, Tim and
- 01:03 Mary just like a list you can pull out a specific item.
- 01:07 And just like a list, they're numbered starting at 0, so 0, 1, and 2.
- 01:11 We save this and run it, we should get John, so that's a tuple.
- 01:16 I mean, the reason why you might want to use a tuple is they
- 01:18 say tuples are a little bit faster than lists.
- 01:22 So if you're doing a huge project at scale, and millions of people are hitting
- 01:26 your website, and you're pulling information from a data, and
- 01:29 slapping it into a list or a tuple, you might want to use a tuple,
- 01:32 because you might get a little bit of a speed bump by using a tuple.
- 01:35 Most of us aren't going to ever be doing projects that big
- 01:38 that you'll even notice that speed difference, but if so, there you go.
- 01:42 The drawback, of course is, you can't change these things.
- 01:45 But we can kind of hack this,
- 01:47 we can get around these things by creating additional tuples.
- 01:51 So let's create tuple_2 and set that equal to Bob.
- 01:56 Now when you just have one item in a tuple, you have to put the comma anyway,
- 02:01 I don't know why, as if you were going to put other items.
- 02:04 And so even if you don't put other items, you still have to do that.
- 02:07 So you can't add Bob to this tuple.
- 02:11 But there is nothing stopping us from creating a third tuple,
- 02:15 tuple_3 and combining these two items into a new tuple.
- 02:21 So we could go tuple_1 + tuple_2 equals tuple_3.
- 02:28 So now if we just print out tuple_3, we get John, Tim, Mary and Bob.
- 02:34 So that's kind of a hacky way to get around.
- 02:36 We haven't added anything new to tuple_1,
- 02:38 we just created a whole new tuple with whatever we wanted to add and
- 02:42 you could do the exact same thing if we wanted to get rid of an item.
- 02:45 You can actually, well, let's take a look here.
- 02:47 Let's go tuple_1 and to access items in a tuple, you give the index number.
- 02:52 So if we wanted number 1, that would print out Tim but
- 02:56 you can also do a range of them so 0, 1, 2, you can go from 1 to 2.
- 03:02 And this is just going to print out Tim because we need,
- 03:05 well let's go from 0 to 2 how about that?
- 03:08 So it starts at 0 and it goes up to 2 but not including 2 so 0, 1,
- 03:13 it'll be John and Tim so if we save this we get John and Tim.
- 03:18 So why are we doing that?
- 03:20 Well, let's say we wanted to create our third tuple and we wanted it to have John,
- 03:25 Tim and Bob.
- 03:26 Essentially we're adding Bob and removing Mary.
- 03:29 So we could go tuple_3 = tuple_1
- 03:33 From 0 to 2, plus tuple_2.
- 03:35 So that'll be John, Tim, and Bob.
- 03:37 Now, if we print out tuple_3, we get John, Tim, and Bob.
- 03:43 John, Tim, and Bob, essentially, we've removed Mary.
- 03:46 Or we could just forget about tuple_2 completely.
- 03:48 If we wanted to, say, remove Mary and just have John and Tim we can create
- 03:53 this tuple_3 and just put this range from 0 to 2, 0, 1.
- 03:58 Remember this range means from 0 up to 2, but
- 04:02 not including 2 which is Mary.
- 04:04 So if we save this and run it, we get John and Tim.
- 04:06 So that's tuples.
- 04:08 Like I said, you can't change a tuple once you've created it, but we've, kind of
- 04:11 learned we can hack the system a little bit and just create a whole new tuple.
- 04:15 And therefore change it so you're going to do that if you want the speed benefit of
- 04:19 using a tuple but like I said unless you're doing something
- 04:22 huge like if you're working at Google on their search algorithm and
- 04:26 it has to be as quick as absolute possible and you know saving those tenths
- 04:30 of a millionth of a millisecond start to matter, we'll use the tuple.
- 04:34 Otherwise you're probably just going to use a list for most things.
- 04:37 So those are tuples.
- 04:38 In the next video we're going to look at dictionaries.
Lesson notes are only available for subscribers.