-3

I was reading this post How to Initialize a 2d Array

I understand that the issue here is that a list is referencing the same list. So if you edit one of the elements. All the references will reflect this change.

But in a case such as:

x = 1
y  = [x]*5
# y = [1, 1, 1, 1, 1]
x = 2
# why doesn't y = [2, 2, 2, 2, 2]??

Shouldn't y change because each element is referencing x?

John Doe
  • 139
  • 1
  • 1
  • 9

2 Answers2

4

You misunderstood. There are 5 distinct references. If those 5 references all point to the same nested list and you alter that nested list, you can see the change reflected through all those references.

You didn't change a nested list here. You changed one of the references.

Think of references as nametags. You can put more than one nametag on an object:

nametag_a+-----------+
                 +---v--+
                 |object|
                 +---^--+
                     |
nametag_b+-----------+

You can 'look' at the object through either reference. Assignment is simply attaching a reference to an object. If a reference pointed to an object before, it is detached from that object and now points to another object:

nametab_b = another_object

results in

nametag_a+-----------+
                 +---v--+
                 |object|
                 +------+

nametag_b    +--------------+
    +-------->another_object|
             +--------------+

The numbered indices in a list are references to; so instead of nametag_a, you have 0, and 1, etc.

The other question talks about nested lists. There you have multiple references to a single list object:

# indices in a list on the left referencing another list

0+---------------------------+
                             |
                             |
1+------------------------+  |
                          |  |
                +---------v--v-----------+
2+--------------> list with more indices |
                +---------^--^-----------+
                          |  |
3+------------------------+  |
                             |
                             |
4+---------------------------+

If you make a change to the list with more indices contents, then you'll see those changes through any of the 5 references in the outer list

Please read Facts and myths about Python names and values by Ned Batchelder, which explains this in more detail.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • does `[x]*5` create a new object that is list containing the value of `x`, and this is why that works like this? – KGS Aug 11 '17 at 06:58
  • 1
    @KGS: Yes, multiplying a list creates a new list object with all the references from the old list copied over. So you had a list with 1 reference, now you have a new list with 5 references. The object that `x` references is not copied, you just get more references. – Martijn Pieters Aug 11 '17 at 07:04
0

Great site to further understanding the situation.

Python Tutor

enter image description here

John Doe
  • 139
  • 1
  • 1
  • 9