0

Whey aren't the two of these the same in python 3.7.1?

1.

all = [[]] * 10
all[3].append(33)

> [[33], [33], [33], [33], [33], [33], [33], [33], [33], [33]]

2.

all = [[] for i in range(10)]
all[3].append(33)
> [[], [], [], [33], [], [], [], [], [], []]

Initially, I tried the first one while expecting the output from the second one but surprisingly this doesn't happen. Any clue why the same reference is initialized in the first snippet?

omid.n
  • 491
  • 5
  • 22
  • 7
    Possible duplicate of [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – Mike Scotty Nov 14 '18 at 09:47
  • 1
    @MikeScotty true, sorry and thanks. – omid.n Nov 14 '18 at 09:49

0 Answers0