Suppose I do the following:
>>> l = [[]]*2 >>> l [[], []] >>> l[0].append(1) >>> l [[1], [1]]
Why does 1 get appended to both lists?
[[]]*2
is a list of two references to the same list. You are appending to it and then seeing it twice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With