-1

I'm a noob in Python. I saw this code and I don't quite understand it.

list_1 = ['History', 'Math', 'Physics', 'CompSci']
list_2 = list_1

print(list_1)
print(list_2)

list_1[0] = 'Art'

print(list_1)
print(list_2)

After the program is executed, I know the list_1 is ['Art', 'Math', 'Physics', 'CompSci'] eventually, but why is the list_2 also ['Art', 'Math', 'Physics', 'CompSci']. I mean, there's no list_2 = list_1 after the second print(list_1). Could someone help me out? Thanks!

Update

I tried some new code.

a = 40
b = a
print(a)
print(b)

a = 50
print(a)
print(b)

And the result is

40
40
50
40

So this means, that problem I mentioned before is only for list, right? If I just have a normal variable like a and b, when I change the value of a, the value of b won't be affected.

Community
  • 1
  • 1
S. Li
  • 767
  • 1
  • 7
  • 14

2 Answers2

4

list_2 = list_1 makes list_2 a reference to the exact same list that list_1 references. Throughout this program there's only one list in memory.

This can be verified when printing the memory address:

list_1 = ['History', 'Math', 'Physics', 'CompSci']
list_2 = list_1

print(id(list_1))
# 5430888
print(id(list_2))
# 5430888

You should definitely read this: https://nedbatchelder.com/text/names.html

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
3

After writing list_2 = list_1, the adress held by the list_1 variable will be the same as the one held by list_2.

As a consequence, every change on list_1 will be seen on list_2 as well, since both variables will represent the same object.

You can check this by comparing the two variables' ids:

>>> id(list_1)
1518664319304
>>> id(list_2)
1518664319304

Or even better:

>>> list_1 is list_2
True
Right leg
  • 16,080
  • 7
  • 48
  • 81
  • So if list_1 and list_2 are variables, then the changes of list_1 won't affect the value of list_2, right? – S. Li May 22 '17 at 10:54
  • @S.Li You need to clearly understand what a variable is. It's like a box, with a number inside. If the variable is an int, a string, a float..., ie a "value type", then the content of the box is this value itself. If the variable is a list, a dictionary... ie a "reference type" or "compound type", then the content of the box is an **adress** to what the variables describes. When one writes `a = b`, the content of `b` is duplicated and goes into `a`. If it's a value, then taking the content of `a` and modifying it will not affest `b`. – Right leg May 22 '17 at 11:21
  • @S.Li On the other hand, if the content of `a` is an address, it cannot be modified as is. Instead, the interpreter will look at this address, and change what's there. But the content of `a` is still the same as `b`'s: that one address. That's why changing what is at the address contained in `a` will be seen from `b` as well: they both refer to the same content. – Right leg May 22 '17 at 11:26
  • I think I finally got it. Thanks for the explanation. That helps a lot! – S. Li May 22 '17 at 11:48
  • @S.Li You're welcome :) Don't forget this. When strange things happen, especially when things change although they shouldn't, odds are great the same thing as here is happening. – Right leg May 22 '17 at 11:50