This lesson is from the Real Python video course by Christopher Bailey.
Lists: Mutable & Dynamic
00:00 For this video, I’m going to show you how lists are mutable and dynamic. So many of Python object types are immutable. Things like an integer or a float, or even a sequence type object like a string—they’re all immutable, meaning that you can’t change the characters within that string or change the value of that integer or that float.
00:20 Once you create that object and you assign it, it’s immutable unless you reassign it. And we’re going to discuss another cousin of a list called the tuple and how it’s immutable also. So, lists are pretty unique. They are mutable.
00:33 Once you create it, elements can be modified, individual values can be replaced, even the order of the elements can be changed. And lists are also dynamic, meaning that you can add elements to the list or remove elements from a list completely. So the list can grow or shrink depending on how you use it.
00:55 Let me have you try it out. A single value in a list can be replaced by indexing and then doing simple assignment. Say you had list
01:07
and then you were to index a particular item—such as in this case, a[2]
, which is 'bacon'
—but instead of just calling it out to have it returned, reassign it. Put an integer, 10
.
01:21
That’s changed it, changed the value within the list. Do another one. Take the last one, change that to an integer of 20
. If you’ve worked with strings before, you might remember that
01:37
if you try to reassign this, the third index, the letter 'b'
in the string, that’ll raise an exception, a TypeError
. It tells you straight up that the 'str' object does not support item assignment
, the way a list does.
01:52
Not only can you change out that item from your list, but you can use the del
(delete) function. And let’s say you delete a[3]
.
02:05
Well, that deleted 'tomato'
right out of that list. If you wanted to change several contiguous elements in a list all at one time, instead of just index assignment, you could do it with slice assignment. Here you’ll put in m
being your first index, and n
being again going up to but not including.
02:25 And then in here you just put an iterable, such as a list. That’s what it would look like. So let’s say we want to change these three.
02:37
So a
has a length of 5
now.
02:42 And then you could just enter a list. Let’s put some floats in.
02:50 And you can see it’s reassigned all three of those.
02:55
And it will replace a slice, let’s say 1
through 4
, which should be these three items here. Let’s say you replaced it with just a single one. Again, a[1:4]
should be those three. So in that case, it’s going to take all three of these items and replace it with a single list item. And if you want to insert multiple items, you can accomplish that too.
03:18
Let’s say between 1
and 2
—basically meaning this item right here, 'Hello'
—you wanted it to be…
03:32
So now you’ve inserted four of these items in the place where it was this one, so the length now of a
is actually 6
.
03:41
Now, this is what we’re talking about the list being dynamic. Python just grows or shrinks the list as needed. Now, one note: if you aren’t using a slice syntax here, and let’s say you just say a[1]
, which is 'a'
here, if you were to replace a[1]
and not a slice with a list, it’s going to insert that as a sublist, because it’s not indicating a slice here. So if you wanted to insert all three of these items differently—so let’s say, again, you might remember del
—and you wanted right in here to insert these three items, well, that would be a[1:1]
04:23
and that would insert all three of these before the rest. Basically a zero-length slice as the index. And if you want to delete multiple items, another way that you could accomplish that—let’s say you want to get rid of all the integers in here. Well, you would say from a[1:5]
. So again, here, a[1:4]
, and then you’d reassign it to an empty list. That would remove those, deleting them. So what about appending items to a list? Well, we were using the concatenation operator earlier.
05:00
Rebuild a
as a list of those strings again. So here’s a
. A version of the concatenation operator that’s also called the augmented assignment operator would allow you to append a couple additional items onto your list,
05:18
putting them at the tail of the list here. What if you wanted some at the beginning? Well, you could do it a little differently. If you assign to a
a list, let’s say with a couple of integers at the beginning, and then concatenated it to a
itself, that would put the two at the beginning, prepending as opposed to appending to the list a
. And so you might say, “Can I just add on a single item?” If you were to +=
and then just add a 30
as an integer to the end of it, it’s going to be mad because it’s not an iterable.
05:53
So you can’t just add a simple integer to it. It has to be an iterable. So that’s where you could use that singleton list. In this case, += [30]
—it doesn’t have a problem with that because type([30])
is a list, compared to type(30)
without square brackets is an integer. Okay, if you’ve worked with strings, you might be aware that a string is an iterable. Okay, what happens if you add it? Let me simplify a
a little bit, make it a little bit shorter. That’s good. So here’s a
, and let’s say you wanted to add 'tomato'
to your list.
06:29 What do you think is going to happen? Well, it might not be the way you thought. In this case, since this is an iterable, it broke it apart into individual characters. You could practice it a little bit here.
06:38
So, this is 3
and we could go up to the end and say that’s equal to an empty list. That should clear those out, right? Okay, perfect. If you wanted to add it more properly, 'tomato'
would have to be inside of a list as a singleton. Next, you get to practice the methods that are available to lists.
You must own this product to join the conversation.