0

I create two lists that seems the same

x = [[]]*4
y = [[],[],[],[]]

since when I print x, y, they give me the same output [[],[],[],[]]. However, when I apply the same operation x[0].append((1,2)), y[0].append((1,2)) on them, they give me different result

print x  >> [[(1, 2)], [(1, 2)], [(1, 2)], [(1, 2)]]
print y  >> [[(1, 2)], [], [], []]

Why this happens? And how to modify x to make sure x and y are exactly the same?

Kevin Powell
  • 571
  • 1
  • 5
  • 18

1 Answers1

3

x is a list that references 4 times the same list.

y is a list of 4 independent empty lists.

hiro protagonist
  • 40,708
  • 13
  • 78
  • 98