0

I recently discovered that when I initialized a list in python using multiplication and when I just did it 'by hand', they weren't behaving the same way when I changed their elements. Like in example code below:

list_normal=[[0,0], [0,0]]
list_multiplication=[[0]*2]*2

list_normal[0][0]=7
list_multiplication[0][0]=7

print(list_normal)         #output:[[7, 0], [0, 0]]
print(list_multiplication) #output:[[7, 0], [7, 0]] 

What is the difference between those two and why do they produce different results?
Also is there a way to create a list of lists of 0, length of which would depend on some variable (like with 'list_multiplication'), that would behave like the normal one?

Butyl
  • 123
  • 4

0 Answers0