0

I have written the python code in the following form

temp=[]
x=[1,2,3]
for i in range(4):
    temp=temp+[x]
temp[1][1]=x[1]+1
print temp[0]
print temp[1]

Here, I wanted the value of just temp[1][1], but the value of temp[0][1] also gets changed. Is there a way of changing just one value? I created a new list and tried to add it to temp, but that does not seem to work as well. Update: Thanks, but it did not seem to work in my case (which was a multi dimensional array). I have the code has follows:

    tempList=[]
    for i in range(openList[0].hx):
          tempList=tempList+[copy.copy(abc)]
    tempList[0][0][0]=123
    print sudokuList

Here abc is a two dimensional list. Modifying the value of tempList[0][0][0] changes the value of tempList[1][0][0] and so on.

Aditya Bhat
  • 95
  • 2
  • 10

3 Answers3

1

That's because of that you are assigning the x to all of your list items so all of them are references to one object and once you change one on them actually you have changed all of them. for getting ride of this problem you can use a list comprehension to define the temp list :

temp=[[1,2,3] for _ in range(4)]
temp[1][1]=7
print temp[0]
print temp[1] 

result :

[1, 2, 3]
[1, 7, 3]
Mazdak
  • 105,000
  • 18
  • 159
  • 188
1

Try, the following. x in for loop is a reference to the original x and not a copy. Because of this reference, changing any element reflects on all objects. So you would need to make a copy as used in following snippet.

temp=[]
x=[1,2,3]
for i in range(4):
    temp=temp+[x[:]]
temp[1][1]=x[1]+1
print temp[0]
print temp[1]

----EDIT----

As per your comment, use copy.deepcopy to copy the list. deepcopy would recursively copy all the referenced elements inside the list. Check copy.deepcopy. So the code looks like:-

import copy
temp=[]
    x=[1,2,3]
    for i in range(4):
        x_copy = copy.deepcopy(x)
        #do something with x_copy. use this inplace of x in your code.
        #will work for 1D or 2D or any other higher order lists.
Barun Sharma
  • 1,452
  • 2
  • 15
  • 20
  • Or he might use `copy(x)` which is a neat solution and is elegant to look at. In python, readability of code is encouraged. and so, I'd say, a copy(x) would be a neater and more readable solution. – Ronnie Sep 20 '15 at 17:24
  • Would it work the same way if x were a 2D array? I am getting the same issue if x is a 2D array. Thanks – Aditya Bhat Sep 20 '15 at 17:28
  • 1
    @Ronnie Yes you are right. @Aditya Either you will have to do this recursively or use `copy`. I will edit the answer accordingly. – Barun Sharma Sep 21 '15 at 06:20
1

This is actually a common error for beginners to Python: How to clone or copy a list?

When you add x to temp four times, you're creating a temp which has the same x four number of times.

So, temp[0], temp[2], temp[3] and temp[4] are all pointing to the same x you declared at the first line.

Just make a copy when adding:

temp=[]
x=[1,2,3]
for i in range(4):
    temp=temp.append(x[:])
temp[1][1]=x[1]+1
print temp[0]
print temp[1]

You can see it with id function, which returns a different value for different objects:

>>> temp=[]
>>> x=[1,2,3]
>>> for i in range(4):
...     temp=temp+[x]
...
>>> id(temp[0]), id(temp[1]) 
(4301992880, 4301992880)     # they're the same

>>> temp=[]
>>> x=[1,2,3]
>>> for i in range(4):
...     temp=temp+[x[:]]
...
>>> id(temp[0]), id(temp[1])
(4301992088, 4302183024)     # now they are not
Community
  • 1
  • 1
utdemir
  • 26,532
  • 10
  • 62
  • 81