0

This is what I want when N=2

lists = [[], []]
print(lists) # [[], []]
lists[0].append(5)
print(lists) # [[5], []]

But, I don't want to repeatedly type N empty lists in the first statement. So, I tried this:

N = 2
lists = [[]] * N
print(lists) # [[], []]
lists[0].append(5)
print(lists) #  [[5], [5]]

I don't understand the behavior displayed by the print above. Please explain the behavior above on creating a list with multiple elements using "*". And of course, I'd like to know how to do this correctly

Balu
  • 527
  • 4
  • 9
  • Duplicate of: [List of Lists Changes Reflected Across Sublists Unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – Lecdi Apr 17 '22 at 17:01

0 Answers0