Let’s have a brief look at what’s really in a data structure.
What Does a Data Structure Contain?
00:00 I’ve got an extra video for you today. Let me create a simple list and I’m going to ask you a silly question. What does this list contain? Am I thinking about, isn’t that obvious?
00:11 2, 5, 6, 7, but does it contain the objects with the integer 2, 5, 6, 7? Or does it contain something else? And if I’m asking the question, you can probably guess that the answer is the more complex one, the second one.
00:28 So it’s perfectly fine to say this list contains the integer 2, 5, 6, 7. But that’s a sort of a casual way of saying it that hides what’s actually happening.
00:40 Data structures in Python do not contain the objects. Instead, they contain references to those objects. Now you might say it’s just terminology, right? Why does it matter? Let’s see why it matters.
00:55
Let me create another list, more_numbers
,
00:59
and let me say that I’m going to put in 3, 5. Now let’s put a list here and let me put another list. So numbers
is a list. So I have a list.
01:10
Now more_numbers
that has an integer, another integer, a list, and another list. Good. more_numbers
3 and 4 are fairly straightforward, right?
01:23
In this case, Python created another list that contains 4, 5, or references to the integers 4 and 5 and more_numbers
contains a reference to this list.
01:36
Now, in this example, the 4, 5 list, it doesn’t really matter because the only way I can access this list is through more_numbers
. There’s no other way at the moment for me to access this list.
01:50
That’s not true for numbers
, however. So first of all, let’s just check that more_numbers
has 3, 4, then the list 4, 5, and then the list 2, 5, 6, 7.
02:02
So what happens if I say more_numbers
[-1]
is a reference to the last list. What if I change the first element of it
02:14
and change it to 999? So notice that more_numbers[-1]
is referencing another list, and therefore [0]
is referencing the first item of that list.
02:27 And I’m saying I want to change it to 999.
02:31
And if I do more_numbers
, unsurprisingly, the first element of the list, which is at the end, is 999. What about numbers
? My original list, what’s happened to it?
02:46
And the answer is it has also changed. And this, unless you’re expecting it, or unless you know that this is happening, can lead to bugs. Because notice how here I’m only changing more_numbers
or so it seems there’s no reference to the original list numbers
, but because more_numbers
in its final element, it does not contain the list.
03:11 It simply contains a reference to an existing object. Therefore, I only have one object, which is the list with 2, 5, 6, and 7. But it has two references.
03:23
The label numbers
refers to this object. And the last element of the list more_numbers
also refers to the exact same object. So if you change one of them, you’re going to be changing the other one as well.
03:38 And this is why it’s important to know that data structures contain references to objects. So when you make changes, be careful if the top object is being referenced somewhere else, you are changing the same object.
03:51 Let me also create a tuple.
03:56 And as you may know, tuples are immutable. So if I create a tuple 4, 5, 6, and then let me put in a list.
04:08 Let’s do different numbers, maybe 9 and 10. A reminder that a tuple doesn’t need parentheses. As long as there are commas, my tuple is still a tuple. When I display it, it shows me the parentheses.
04:22 Now tuples are immutable. This means if I try to change the first value to 999, Python is going to say, no, no, no, no. You can’t do that. Tuple object does not support item assignment.
04:36 How about if I take the last item of my tuple, which you may recall is a list.
04:44 Now a list is mutable, so I should be able to change the first value, oops, of that list.
04:54 And the immutable tuple appears to have changed.
05:00 So beware when we say a tuple is immutable. Now you might be thinking, oh, does that mean that a tuple is not immutable? No, the tuple is still immutable. Why?
05:09 Because the tuple, where did I create it there? When I created this tuple, I created references to the integer 4, 5, 6, and then a reference to the list that contains 9 and 10.
05:23 The tuple contains the references, and those references have not changed.
05:30 This tuple still contains the same references as when I created it. The difference is that the last reference, which points to a list, it’s the same list, but the value of the list has changed.
05:46 So data structures in Python, in many instances, it’s fine to say my tuple contains a list, but it actually contains a reference to that list. Data structures contain references to objects.
You must own this product to join the conversation.