0

Question on list in python. Why this output?

A = [ [ ] ] * 3 A[0]. append(3) print(A) Why it prints [[3],[3],[3]] and why not [[3], [ ], [ ] ]

20th Feb 2020, 6:49 AM
Peter Parker
Peter Parker - avatar
1 Resposta
+ 4
Because when using times 3, it copies the reference to the empty list So A == [[], [], []] but A[0] == A[1] == A[2] So when you change any one, the same is reflected in the other two, because they are referencing the same list. https://code.sololearn.com/cDNDoUBxcniR/?ref=app
20th Feb 2020, 6:53 AM
Gordon
Gordon - avatar