Creating nested lists in different ways gives different results when appending in python3

Viewed 10

I am working through a problem where I'm trying to downsample a 2D velocity, and had come up with using nested lists as the appropriate way to do a middle part of my problem, however I found that when trying to append to the nested lists, I ran into the error where it would append to all the elements of the list by accident.

I would normally use arrays, but because the downsampling isn't happening onto a rectangular coordinate system, then the different elements would have different numbers of values making them up, which is why I went for the lists.

Here is my example, working in python 3.7.1

# the way I initially tried to initialise an 8x8 empty array
vx1 = [[[]]*8]*8
print(vx1)
>>> [[[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []]]
# appending to an element in the middle, which was normally done
# with a loop determing which element to append to
vx1[4][2].append(3.14)
print(vx1)
>>> [[[3.14], [3.14], [3.14], [3.14], [3.14], [3.14], [3.14], [3.14]],
     [[3.14], [3.14], [3.14], [3.14], [3.14], [3.14], [3.14], [3.14]],
     [[3.14], [3.14], [3.14], [3.14], [3.14], [3.14], [3.14], [3.14]], 
     [[3.14], [3.14], [3.14], [3.14], [3.14], [3.14], [3.14], [3.14]],
     [[3.14], [3.14], [3.14], [3.14], [3.14], [3.14], [3.14], [3.14]],
     [[3.14], [3.14], [3.14], [3.14], [3.14], [3.14], [3.14], [3.14]],
     [[3.14], [3.14], [3.14], [3.14], [3.14], [3.14], [3.14], [3.14]],
     [[3.14], [3.14], [3.14], [3.14], [3.14], [3.14], [3.14], [3.14]]]

And I can't figure out why it does this. The only thing I did notice was that if I initialise my list different, it doesn't do this. Example below.

# the way I now initialise the list
vx2 = [[[], [], [], [], [], [], [], []], 
       [[], [], [], [], [], [], [], []], 
       [[], [], [], [], [], [], [], []], 
       [[], [], [], [], [], [], [], []], 
       [[], [], [], [], [], [], [], []], 
       [[], [], [], [], [], [], [], []], 
       [[], [], [], [], [], [], [], []], 
       [[], [], [], [], [], [], [], []]]
print(vx2)
>>> [[[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []]]
vx2[4][2].append(3.14)
print(vx2)
>>> [[[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [3.14], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []],
     [[], [], [], [], [], [], [], []]]

Which is the intended behaviour. Can anyone explain to me what the difference between my two initialisations is?

0 Answers
Related