This question already has answers here:
why can't I change only a single element in a nested list in Python [duplicate]
(2 answers)
Closed 8 years ago.
I have been using python for a lot and today I get surprised by a simple nested list.
How can I change the value of an element of my list?
>>> l=[[0,0]]*10
>>> l
[[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
>>> l[0]
[0, 0]
>>> l[0][0]
0
>>> l[0][0]=1 #here comes the question
>>> l
[[1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0]]
>>>
I would expect as final result
l=[[1,0],[0,0],[0,0]...]
Why it does not append? (I would like a theoretical explanation )
How can I get the desired result?
Thanks.
EDIT:
In starting the list in another way I get the desired result
>> l=[[0,0],[0,0]]
>>> l
[[0, 0], [0, 0]]
>>> l[0][0]=1
>>> l
[[1, 0], [0, 0]]
>>>
but I am still wondering why the first version does not work and how can I initialize a list of a lot of elements?
You have a list of the same [0, 0] element 10 times here:
l=[[0,0]]*10
Any time you modify one, it modifies them all, because they're the same list.
One safe way to make them unique would be:
l = [[0, 0] for _ in range(10)]
One easy way to check would be to print the id of each, which is the memory address where it's stored:
>>> for element in l:
... print id(element)
...
34669128
34669128
34669128
34669128
34669128
34669128
34669128
34669128
34669128
34669128
Related
This question already has answers here:
What is the difference between shallow copy, deepcopy and normal assignment operation?
(12 answers)
Closed 3 years ago.
When I run this,
key=[[0, 0, 0], [1, 0, 0], [0, 1, 1]]
key_=[[0, 0, 0], [1, 0, 0], [0, 1, 1]]
m=3
for i_ in range(1,m+1):
for j_ in range(1,m+1):
key[j_-1][m+1-i_-1]=key_[i_-1][j_-1]
print(key,key_,sep='\n')
I got this:
>>> [[0, 1, 0], [1, 0, 0], [1, 0, 0]]
[[0, 0, 0], [1, 0, 0], [0, 1, 1]]
However, when I changed only the second line,
key=[[0, 0, 0], [1, 0, 0], [0, 1, 1]]
key_=key
m=3
for i_ in range(1,m+1):
for j_ in range(1,m+1):
key[j_-1][m+1-i_-1]=key_[i_-1][j_-1]
print(key,key_,sep='\n')
I got this:
>>> [[0, 1, 0], [0, 0, 0], [0, 0, 0]]
[[0, 1, 0], [0, 0, 0], [0, 0, 0]]
Why do the two codes have different results?
In my opinion, they should be same.
Why the different parts make different results?
In Python, variables which contain lists are more like pointers. When you say key_=key, you're telling Python to use the same list in both cases, not a copy. If you want a copy of the original list, use key_=[x.copy() for x in key]. This will copy the contents of each item (which are lists) in key.
Examples:
Make two lists which contain 1, 2 and 3
>>> my_list = [1,2,3]
>>> my_list
[1, 2, 3]
>>> my_list2 = my_list
>>> my_list2
[1, 2, 3]
Now let's edit the value of the original list...
>>> my_list[0] = 10
>>> my_list
[10, 2, 3]
>>> my_list2
[10, 2, 3]
The changes are copied because my_list and my_list2 are the same, not just a copy of it.
Let's change my_list2:
>>> my_list2[1] = 20
>>> my_list2
[10, 20, 3]
>>> my_list
[10, 20, 3]
And once again, the values are updated between the two as they both point to the same list.
Now let's see what happens if we use the copy method:
>>> my_list_copy = my_list.copy()
>>> my_list_copy
[10, 20, 3]
>>> my_list[0] = 1
>>> my_list
[1, 20, 3]
>>> my_list_copy
[10, 20, 3]
And so we can see that the two lists start with the same contents, but are different.
Using the is operator, we can also see this difference between the 3 list variables:
>>> my_list is my_list2
True
>>> my_list is my_list_copy
False
>>>
EDIT:
key is a list containing lists as items. When the copy method is called, only the outer list is copied, so the actual items in both lists (ie the sub-lists) are identical. To copy these by value rather than effectively by reference, we can use a simple list comprehension:
key_=[x.copy() for x in key]
This code copies each item in key by value, and creates a new list with these as items.
The output using this is
[[0, 1, 0], [1, 0, 0], [1, 0, 0]]
[[0, 0, 0], [1, 0, 0], [0, 1, 1]]
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 3 years ago.
Let's say that I make a 3D list
list = [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
and I run
list[0][0][0] = 1 #set the first element of the first list of the first list to 1
print(list)
I'd expect to get
[[[1, 0], [0, 0]], [[0, 0], [0, 0]]]
but instead, I get
[[[1, 0], [1, 0]], [[1, 0], [1, 0]]]
Can someone figure out how to make it assign a variable to ONLY ONE element of a 3D list, instead of every first element? Thanks!
If it matters, I'm using Python 3.7 32-bit.
I have reproduced your results by making an assumption about how you actually defined your list. I assume that you defined some variable such as ab below and used that to create your list. However, the new list is still a bunch of references to your ab variable, so changing one actually changes ab which will affect your whole list.
ab = [0,0]
mylist = [[ab,ab],[ab,ab]]
mylist[0][0][0] = 1
print(mylist," ",ab)
OUTPUT
[[[1, 0], [1, 0]], [[1, 0], [1, 0]]] [1, 0]
To resolve this, simple initialize your lists with 0 instead of some variable:
mylist = [[[0,0],[0,0]],[[0,0],[0,0]]]
or
mylist = [[[0 for _ in range(2)] for _ in range(2)] for _ in range(2)]
This question already has answers here:
Copying nested lists in Python
(3 answers)
Closed 8 years ago.
I'm making a program that requires and editable temp array that does not affect the original. However, whenever I run the function and test it, it edits the actual array like so:
x = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
y = copying(x)
y[0][0] = 1
print(x)
[[1, 0, 0], [0, 0, 0], [0, 0, 0]]
Here is the function:
def copying(array):
temp = []
for i in array:
temp.append(i)
return temp
The function works for flat lists, but the array entry doesn't work. Is there an alternative that I should use? (I have attempted list() and copy())
You need to use the function deepcopy from copy module:
copy.deepcopy(x)
Return a deep copy of x.
This function is copying everything, even sub elements (and sub sub elements and... you understand I think). Your short example corrected:
>>> from copy import deepcopy
>>> x = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> y = deepcopy(x)
>>> y[0][0] = 1
>>> x
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> y
[[1, 0, 0], [0, 0, 0], [0, 0, 0]]
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python List Index
result=[range(3)]*2
for i in range(len(result)):
result[i][2]=4*i
print result
I would expected [[0, 1, 0], [0, 1, 4]]
Why do I get [[0, 1, 4], [0, 1, 4]]
Thank you!
When you do [range(3)] * 2, it makes a list with two references to the same list inside, so modifying result[0] and result[1] each modify both.
Use [range(3) for i in range(2)] to make a list with two different results of range(3) in it.
List "result" is: [[0, 1, 2], [0, 1, 2]]
Your iteration is: "for i in range(len(result))"
len(result) is: 2
range(2) is: [0,1]
meaning:
first time:
result[0][2]=4*0
second time:
result[1][2]=4*1
which gives you the result [[0, 1, 4], [0, 1, 4]]
This is what is doing step by step.
If you add a "break" to the iteration you see the result is [[0, 1, 0], [0, 1, 0]]
The "result" list works by reference. When it is called, it is pointing to the same object.
I have constructed a 3 level nested list
#run on Python 3.2.3 32 bit on Win 7
L2=list(0 for i in range(2))
L3=list(L2 for i in range(3))
L4=list(L3 for i in range(4))
#give a new value to the very first number in list:
L4[0][0][0]=5
print("L4:")
print(L4)
#outputs erronously:
#[[[5, 0], [5, 0], [5, 0]], [[5, 0], [5, 0], [5, 0]], [[5, 0], [5, 0], [5, 0]], [[5, 0], [5, 0], [5, 0]]]
The same list given explicitly
#the same L4 given explicitly:
anotherL4=[[[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]]]
print("anotherL4:")
#give a new value to the very first number:
anotherL4[0][0][0]=5
print(anotherL4)
#outputs correctly:
#[[[5, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]]]
You're wrong. You've copied the reference multiple times, which means they're actually all the same list.
When you write list(L3 for i in range(4)), you are telling it to yield the same list L3 on each iteration of the generator comprehension. When you subsequently modify that list, the modifications show up everywhere, because all of them are references to the same list.
You could get the effect you seem to want by doing
list(list(L3) for i in range(4))
since using list(L3) makes a new list.
Just to elaborate,
a = 1
b = 2
c = [a,b]
a += 1
print c
Your problem is that you built a list of list references rather than a list of lists. Since the references all pointed back to a single list, when you mutate that single list, all the references show the change.
L0 = range(3)
L1 = range(3)
print(id(L0)) # prints a number
print(id(L1)) # prints a different number
print(id(L0) == id(L1)) # prints False
print(L0 is L1) # prints False; not same objects
print(L0 == L1) # prints True; values match
# your original code:
L2=list(0 for i in range(2))
L3=list(L2 for i in range(3))
L4=list(L3 for i in range(4))
print(L3[0] is L2) # prints True; L3[0] is a reference to L2
We can fix it and explicitly show what we are doing by using copy.deepcopy():
import copy
L2 = [0 for i in range(2)]
L3 = [copy.deepcopy(L2) for i in range(3)]
L4 = [copy.deepcopy(L3) for i in range(4)]
#give a new value to the very first number in list:
L4[0][0][0]=5
print("L4:")
print(L4)
Note that instead of making a generator expression and passing it to list() to force it to be expanded out to a list, I just used list comprehensions in the above code to directly make lists.
More usually if you want to do this crazy thing, you should maybe just nest some list comprehensions:
L4 = [[[0 for _ in range(2)] for _ in range(3)] for _ in range(4)]
This makes it pretty clear that we are building a new list of lists of lists. And if you use copy.deepcopy() you are basically just copying a bunch of zeroes, so you might as well just build new lists using zeroes.