search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

0 answers
Q&A List of lists changes reflected across sublists unexpectedly

0

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

Your Answer
submit

0 answers

Popular tool

vc9-vc14 (32+64 bit) runtime library collection (link below)

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

VC9 32-bit phpstudy integrated installation environment runtime library

PHP programmer toolbox full version

PHP programmer toolbox full version

Programmer Toolbox v1.0 PHP Integrated Environment

VC11 32-bit

VC11 32-bit

VC11 32-bit phpstudy integrated installation environment runtime library

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use