2D list has weird behavor when trying to modify a single value
Question:
When I try this code:
data = [[None]*5]*5
data[0][0] = 'Cell A1'
The value of data
ends up like:
[['Cell A1', None, None, None, None],
['Cell A1', None, None, None, None],
['Cell A1', None, None, None, None],
['Cell A1', None, None, None, None],
['Cell A1', None, None, None, None]]
Why does the 'Cell A1'
value appear in every nested list?
Answers:
This makes a list with five references to the same list:
data = [[None]*5]*5
Use something like this instead which creates five separate lists:
>>> data = [[None]*5 for _ in range(5)]
Now it behaves as expected:
>>> data[0][0] = 'Cell A1'
>>> print(data)
[['Cell A1', None, None, None, None],
[None, None, None, None, None],
[None, None, None, None, None],
[None, None, None, None, None],
[None, None, None, None, None]]
In Python, every variable is an object, and thus a reference. You first created a list of 5 None
s, and then you build a list with 5 times the same object.
As explained in the documentation for sequence types (which includes lists):
Note also that the copies are shallow; nested structures are not copied. This often haunts new Python programmers; consider:
>>> lists = [[]] * 3
>>> lists
[[], [], []]
>>> lists[0].append(3)
>>> lists
[[3], [3], [3]]
What has happened is that [[]]
is a one-element list containing an empty list, so all three elements of [[]] * 3
are (pointers to) this single empty list. Modifying any of the elements of lists
modifies this single list. You can create a list of different lists this way:
>>> lists = [[] for i in range(3)]
>>> lists[0].append(3)
>>> lists[1].append(5)
>>> lists[2].append(7)
>>> lists
[[3], [5], [7]]
When I try this code:
data = [[None]*5]*5
data[0][0] = 'Cell A1'
The value of data
ends up like:
[['Cell A1', None, None, None, None],
['Cell A1', None, None, None, None],
['Cell A1', None, None, None, None],
['Cell A1', None, None, None, None],
['Cell A1', None, None, None, None]]
Why does the 'Cell A1'
value appear in every nested list?
This makes a list with five references to the same list:
data = [[None]*5]*5
Use something like this instead which creates five separate lists:
>>> data = [[None]*5 for _ in range(5)]
Now it behaves as expected:
>>> data[0][0] = 'Cell A1'
>>> print(data)
[['Cell A1', None, None, None, None],
[None, None, None, None, None],
[None, None, None, None, None],
[None, None, None, None, None],
[None, None, None, None, None]]
In Python, every variable is an object, and thus a reference. You first created a list of 5 None
s, and then you build a list with 5 times the same object.
As explained in the documentation for sequence types (which includes lists):
Note also that the copies are shallow; nested structures are not copied. This often haunts new Python programmers; consider:
>>> lists = [[]] * 3 >>> lists [[], [], []] >>> lists[0].append(3) >>> lists [[3], [3], [3]]
What has happened is that
[[]]
is a one-element list containing an empty list, so all three elements of[[]] * 3
are (pointers to) this single empty list. Modifying any of the elements oflists
modifies this single list. You can create a list of different lists this way:>>> lists = [[] for i in range(3)] >>> lists[0].append(3) >>> lists[1].append(5) >>> lists[2].append(7) >>> lists [[3], [5], [7]]