I created a list of lists:
>>> xs =
Then, I changed one of the innermost values:
>>> xs[0][0] = 5 >>> print(xs)
I expected this to only affect the first sublist, not all of them. That is:
>>> print(xs)
Why did every first element of every sublist change to 5?
See also:
-
How do I clone a list so that it doesn't change unexpectedly after assignment? and
How to initialize a two-dimensional array (list of lists, if not using NumPy) in Python? for workarounds for the problem
-
List of dictionary stores only last appended value in every iteration for an analogous problem with a list of dicts
How do I initialize a dictionary of empty lists in Python? for an analogous problem with a dict of lists
0 answers
Hot tools Tags
Hot Questions
Popular tool
vc9-vc14 (32+64 bit) runtime library collection (link below)
Download the collection of runtime libraries required for phpStudy installation
VC9 32-bit
VC9 32-bit phpstudy integrated installation environment runtime library
PHP programmer toolbox full version
Programmer Toolbox v1.0 PHP Integrated Environment
VC11 32-bit
VC11 32-bit phpstudy integrated installation environment runtime library
SublimeText3 Chinese version
Chinese version, very easy to use
Hot Topics
20518
7
13631
4






