0

I creat a 2d array using two methods they look the same

helper1=[[]]*5
helper=[[] for i in range(5)]

when I append new element to the arrays, they behave differently

helper1[0].append(1) 
result is [[1], [1], [1], [1], [1]]
helper[0].append(1)
result is [[1], [], [], [], []]

I am surprised by the first outcome , why it is so? thanks !

laughter
  • 7
  • 3
  • Does this answer your question? [Element (array) "multiplication" vs. list comprehension initialization](https://stackoverflow.com/questions/18933257/element-array-multiplication-vs-list-comprehension-initialization) – JonSG Mar 31 '22 at 15:18
  • 1
    @jonSG thank you! I guess something like this, was surprised – laughter Mar 31 '22 at 15:22

0 Answers0