0

I want to change only one element in the 2d list. I can change an element in list1 using list1[0][2] = "x" but when i do the same for list2 more than one element is changed.

list1 = []

for i in range(0,5):
    list1.append(['O']*5)


list2 = [['o','o','o','o','o']]*5

1 Answers1

0

Because this is 5 copies of the same list

list2 = [['o','o','o','o','o']]*5

Getting a good understanding of when it's ok to use copies of the same reference, and when it's not ok is important to writing efficient code.

John La Rooy
  • 295,403
  • 53
  • 369
  • 502