This course will be retired on January 6, 2020.
Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Lists are great but sometimes you want to be sure your data is safe and unchangeable. Python gives us tuples for exactly this use (and a lot more).
I didn't mention it in the video because it should be an assumption by now, but you can use del
to delete an entire tuple. You cannot delete just a single item from the tuple, though, of course, since tuples are immutable.
-
0:00
[MUSIC]
-
0:04
While it might not seem like a problem,
-
0:06
one of the biggest drawbacks to lists is that they're mutable or editable.
-
0:09
Why is mutability a problem?
-
0:11
Most of the time, it's not.
-
0:13
You can go a long way with lists and their flexible data.
-
0:16
Sometimes though,
-
0:17
it's really important that our data stay exactly how we created it.
-
0:20
It has to stay in exactly the same order.
-
0:22
And each member should keep its value.
-
0:24
That's where tuples come in.
-
0:26
Or tuples.
-
0:27
Honestly, I'm not sure which is the right way.
-
0:28
But I like tuples.
-
0:29
So that's what I'm going to say.
-
0:31
Tuples are like lists in that each member has an index.
-
0:34
They can be looped over.
-
0:35
And each member can be pretty much any other data type that you want to use.
-
0:39
Tuples, though, can't be changed in place.
-
0:41
They're immutable.
-
0:42
This also makes them a bit more memory efficient, which is always nice win.
-
0:46
All right, time for tuples in work spaces.
-
0:48
One of the first really interesting and
-
0:50
different things about tuples is how they're constructed.
-
0:54
We make lists with brackets.
-
0:56
All right?
-
0:57
And commas.
-
0:58
Or well, brackets and commas.
-
1:00
We make dictionaries with braces and colons.
-
1:04
And strings, you make with quote marks, double or single.
-
1:07
Most of the time,
-
1:08
you'll see people creating tuples with parentheses and commas.
-
1:11
So you'll see like, my_tuple = {1,2,3}.
-
1:16
And I look at my tuple.
-
1:18
It's a tuple, right?
-
1:20
And has parenthesis.
-
1:21
And if you ask a random Python user if the parenthesis make the tuple.
-
1:25
There is an all right chance that they're going to say yes.
-
1:28
Because you almost always see them with parentheses.
-
1:30
But really it's the comma that makes it a tuple.
-
1:34
See, check this out.
-
1:36
I do my second tuple equals one comma to comma three.
-
1:42
And then I look at my second tuple.
-
1:45
Still 1, 2, 3. Now this is also why we have
-
1:48
to include a comma.
-
1:49
When I make a tuple with just a single item.
-
1:51
Otherwise, we get whatever item we wanted to put in the tuple.
-
1:54
Right. If I do my_third_tuple.
-
1:57
And I did (5).
-
2:00
If I look at my_third_tuple, I just get the number 5.
-
2:04
This was because Python lets you use parentheses to mark something as a single
-
2:07
bit of data, even if it takes up multiple lines.
-
2:10
Try this with a string gets really handy, but let's go back to tuples.
-
2:14
For instance, if I want this one to actually be a tuple,
-
2:16
I have to put a comma in there.
-
2:17
And now, my third tuple is actually a tuple.
-
2:21
So feel free to create your tuples either way, with parenthesis or without.
-
2:25
Often, the parenthesis make your tuples more obvious, so they're a good idea.
-
2:29
Just like with lists and strings, we can make tuples with the tuple function too.
-
2:34
So, my fourth tuple = tuple, and
-
2:38
then I'll put in a list of (1, 2, 3).
-
2:43
And, now my fourth tuple is the tuple (1, 2, 3).
-
2:48
Now I said that tuples are immutable.
-
2:50
Just to test, let's try and change one of the numbers in our tuples.
-
2:53
So, my tuples 0 is = to 5.
-
2:58
And, I get a type error.
-
3:00
Because tuple object does not support item assignment.
-
3:03
So cool, okay.
-
3:06
And I get the same thing if I try to do my_tuple[0] += 2,
-
3:10
because it doesn't support item assignment.
-
3:13
So, okay, can't assign things to specific indexes of a tuple.
-
3:17
And if we do dir on a tuple, so I'll do dir(my_tuple).
-
3:22
Then we can see that we don't have nearly as many ways to change them as lists do.
-
3:29
So okay.
-
3:29
There's all of our stuff that we can do.
-
3:31
Now, we can count tuples and find the index of a member, right.
-
3:35
We can count them, we can find the index of something.
-
3:38
But we can't add items.
-
3:40
We can't pop items off.
-
3:41
And we can't even call del on our tuples.
-
3:44
One fun little gotcha with tuples though,
-
3:47
is that you can change the values inside of mutable tuple members.
-
3:52
That's a lot of jargon, so let me just show you.
-
3:54
So let's make tuple with a list.
-
3:57
And it's going to be one.
-
3:59
And then the string apple.
-
4:01
And then a list of three four and five.
-
4:05
And if I look at couple with a list.
-
4:09
I see those items.
-
4:10
Right? So,
-
4:11
if I try to change immutable objects inside the tuple.
-
4:14
Well, you can guess what happens here.
-
4:15
Tuple with a list zero equals five, we've already done this.
-
4:19
Right? We get item assignment.
-
4:21
Which is correct.
-
4:22
Because numbers are immutable.
-
4:24
I get the same result if I try to change, say, number one which is currently apple.
-
4:30
If I want to change that to banana.
-
4:32
Then I get my type error again.
-
4:34
Because strings are immutable, right.
-
4:37
But lists are mutable.
-
4:39
So tuple with a list.
-
4:42
And let's try to change two and one.
-
4:46
And I want to make that a seven.
-
4:49
So now if I look at tuple with a list.
-
4:52
I have three seven five.
-
4:55
Instead of three four five, so... awesome.
-
4:58
The only thing that I can't do is actually remove that list.
-
5:01
Because that would be changing the tuple.
-
5:03
I can do whatever I want to the inside of the list, though.
-
5:07
So tuples seem to be heavily restricted lists.
-
5:10
Why would you want to use them?
-
5:11
Tuples have a fixed size and
-
5:13
memory, which is great if you want to reduce the memory footprint of a script.
-
5:16
But they also have a couple of surprising uses for swapping values around.
-
5:19
And they're great for packing and unpacking.
-
5:21
Let's check those out in our next video.
You need to sign up for Treehouse in order to download course files.
Sign up