+ 1
Can someone please explain this to me?
It's about Python lists, I can't understand it😔 https://code.sololearn.com/czvfwSVgmFPA/?ref=app
1 Resposta
+ 2
Lists store the adresses of the items they contain, and they have an identity.
x = [[0]*2]*2
[0]*2 creates a list with two zeros.
[[0]*2] creates a list that contains such a list.
And with * 2 you basically say: 'Give me a list that contains two times the address of the inner list.'
So although it looks like a list that contains two lists, it actually contains the address of the same list twice.
So if you change one of the first list's elements, it changes in the second list too - because in reality it is the very same list.