Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14155

List of lists changes reflected across sublists unexpectedly

$
0
0

I created a list of lists:

>>> xs = [[1] * 4] * 3>>> print(xs)[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]

Then, I changed one of the innermost values:

>>> xs[0][0] = 5>>> print(xs)[[5, 1, 1, 1], [5, 1, 1, 1], [5, 1, 1, 1]]

Why did every first element of each sublist change to 5?


See also:


Viewing all articles
Browse latest Browse all 14155

Trending Articles