1

I was using lists in python and I noticed something I cannot explain.

my_list1 = [[],[],[]]
my_list2 = [[]]*3

my_list1[0].append(5)
my_list2[0].append(5)

my_list1[0].append(5) behaves as expected. [[5],[],[]]

But for my_list2[0].append[5] gives the result. [[5],[5],[5]]

Essentially both the lists are same but why does the append() behave differently on both?

Patrick Haugh
  • 55,247
  • 13
  • 83
  • 91

0 Answers0