<s>[INST] Python list of lists, changes reflected across sublists unexpectedly I needed to create a list of lists in Python, so I typed the following:  
myList = [[1] * 4] * 3

The list looked like this:  
[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]  

Then I changed one of the innermost values:  
myList[0][0] = 5

Now my list looks like this:  
[[5, 1, 1, 1], [5, 1, 1, 1], [5, 1, 1, 1]]  

which is not what I wanted or expected. Can someone please explain what's going on, and how to get around it?
 [/INST] When you write [x]*3 you get, essentially, the list [x, x, x]. That is, a list with 3 references to the same x. When you then modify this single x it is visible via all three references to it.
To fix it, you need to make sure that you create a new list at each position. One way to do it is
[[1]*4 for n in range(3)]

 </s>