I'm trying to change an item in a nested list. I thought this would be very straightforward. I have the following:
temp = [1, 2, 3, 4]
my_list = [temp for i in xrange(4)]
print "my_list = ", my_list
out: my_list = [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
So just a normal list of lists. I want to access one item:
print my_list[0][1]
out: 2
As expected. The problem comes when changing the item. I just want to change item in my_list[0][1], but I get the following:
my_list[0][1]= "A"
print my_list
out: [[1, 'A', 3, 4], [1, 'A', 3, 4], [1, 'A', 3, 4], [1, 'A', 3, 4]]
Why is it changing the four positions, instead of just one? How to avoid it?