0
a = [[],[],[],]
a[1].append(5)
print(a)

Above Code will print the output as [[ ], [5], [ ]]

a = [[]]*3
a[1].append(5)
print(a)

Above Code will print the output like [[5], [5], [5]]

Could anyone please explain to me why there are '5' in all the lists as I have appended it only first list.

ABHINAV MEHTA
  • 25
  • 1
  • 5
  • `[[]] * 3` replicates the item in the list 3 times. It is the *same* list. `[[] for _ in range(3)]` would create 3 *different* lists. – Mark Tolonen Feb 22 '22 at 03:28

0 Answers0