Switching to Mutable Built-in Data Types: Lists
00:00 Let’s now look at Python’s built-in mutable types. And we can start with the lists. So you can create a list of digits, 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
00:13
A list is mutable, which means we can change its values as you’ve seen before. But first, let’s look at the identity of digits
. So what is the identity of the object that the variable name digits
is pointing to?
00:28 And CPython gives us a number, which is the memory location of this object, ends with 8688. We’re going to use this to see whether the object changes to a new object or stays the same.
00:42
Let’s pick a digit, for example, the last one, and we’d like to change it to 100
instead of 9
. So digits
is now 0, 1, 2, 3, 4, 5, 6, 7, 8, and 100.
00:55
But the question is, is this the same object or is it the new object? It is the same object. The identity is still the same number ending in 8688
.
01:06
Therefore, the same object, the object that has the same identity that’s in the same location and memory can change. In this case, you change the last value from 9
to 100
and you can have a look at what happens with some of the list methods.
01:22
For example, let’s now append 200
to the list digits
. So digits
is now the same as before, but there’s an extra 200
at the end and the identity of digits
has not changed.
01:34
It’s the same number ending in 8688
. We can delete one of the numbers. For example, delete the first number from digits
. So digits
is now starts with 1
, but the identity is still the same.
01:49 So a mutable object such as a list remains the same object. It has the same address and memory, same location, but its contents can change. That’s what makes it mutable.
02:02
And one final example, let’s use digits.sort
. But since the list I’m showing you is already sorted, let’s use reverse=True
.
02:14 And this reverses the list,
02:16 but it still remains the same object. So even though all the items have changed place because they’re reverse from the original version, it’s still the same object that has mutated, that has changed into this new form.
02:33
And how about if we use the +=
operator. In an earlier lesson, you used the +=
operator with a tuple. And you’ve seen that this creates a new object.
02:44 Let’s see whether the behavior is the same or different with lists. Let us add, we need to add a list to the list `digits, let’s go for 12, 13, 15
02:56
and digits
is now the list, the reverse list we had 200, 100 all the way up to 1, and then 12, 13, and 15. And the question is, is this the same object as before?
03:07
And you can see that the identity of digits
is still the number that ends with 8688. This means that +=
keeps the same object. It does not create a new object. Why?
03:21
Because a list is mutable. This is different, however, to the following: digits = digits +
and let’s this time add 20, 21 and 22. So digits
is the list as it was before, and 20, 21, 22 at the end.
03:42
But, look at the identity of digits
. It’s now a different number. It ends with 6672. Why? This situation is different. On the right hand side of the =
, you have the name digits, which is referring to the list, the object whose identity ends with 8688.
04:01
But then you are using a +
to add another list to it so this creates a new list. `digits + [20, 21, 22] creates a new object, which is a list containing all the numbers.
04:14
This new object, you are then reassigning it to the same name digits
. Therefore, at the end of this statement, the variable name digits
is now pointing to a new object, the one you created from the old object plus the new list.
04:30
That’s why digits = digits +
[20, 21, 22
] is not the same as ‘+=` because one creates a new object, whereas ‘+=` mutates the existing object.
Become a Member to join the conversation.