5
I’m studying lists and there’s a behavior I don’t understand.
I know that when I equate one list with another, a connection between them is created. And that when I slice a list, I create a copy of the list (no connection between them, that is, when it changes in changes, it does not change in another). However, when there are lists within lists, even with slicing, connection is made between lists.
For example:
>>> a = [[2, 3, 5], [1, 3, 5]]
>>> b = a[:]
>>> print(b)
[[2, 3, 5], [1, 3, 5]]
>>> b[0][1] = 100
>>> print(a[0][1])
100
>>> print(b)
[[2, 100, 5], [1, 3, 5]]
>>> print(a)
[[2, 100, 5], [1, 3, 5]]
I mean, it’s changed on the list b
, also changed on the list a
.
Why did this happen?
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.
– Maniero