0

Why does this creates aliased arrays ?!! is this a expected behavior ?

In [57]: p=[[]] * 3

In [58]: p[0].append(2)

In [59]: p
Out[59]: [[2], [2], [2]]
sten
  • 7,028
  • 9
  • 41
  • 63
  • 1
    It is full copy of `p = [list()] * 3` which creates single instance of `list` and copy reference to this instance 3 times. Yes, **it is** expected behaviour. To do what you want you can use list comp: `[[] for _ in range(3)]` – Olvin Roght Jul 10 '20 at 19:49
  • 1
    Does this answer your question? [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – AMC Jul 10 '20 at 19:50
  • yeah i used range and for – sten Jul 10 '20 at 20:02

0 Answers0