This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 7 years ago.
It might be a very stupid question but I just don't understand this code:
>>> a=[[0]*3]*3
>>> a
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> a[0][1]=3
>>> a
[[0, 3, 0], [0, 3, 0], [0, 3, 0]]
Why a call a[0][1] will initialize the whole second column to be 3?
This is because a[0][1] actually is the reference to the same lists. When you use * operator to duplicate elements into a list, these duplicate elements are not individually stored in the memory. They point to the same list, so if you modify one list, all the lists would be modified.
Related
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 12 months ago.
I have previously worked in C, I am facing problem in assigning value in 2d list
graph = [[0]*3]*3
print(graph)
graph[0][1] = 3
print(graph)
Output
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[0, 3, 0], [0, 3, 0], [0, 3, 0]]
Expected output :
[[0, 3, 0], [0, 0, 0], [0, 0, 0]]
Is there any way to assign values other than using numpy array as answered in
Assigning values Python 2D Array
you can use a for loop to do this simply
a = []
for i in range(3):
a.append([0]*3)
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 2 years ago.
i am puzzled by this behavior
r,c = (5,2)
slist = [[0]*c]*r
print(slist)
for i in range(r):
slist[i][0] = i
print(slist)
Output is
[[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
[[4, 0], [4, 0], [4, 0], [4, 0], [4, 0]]
When you do [[0] * c] * r, you create a list where every element is a reference to the same list. So, when you change one, they all change. Use a list comprehension with a range instead to create unique lists:
slist = [[0] * c for _ in range(r)]
See here for more info.
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:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 5 years ago.
Why does this happen?
In [1]: a=[[0]*2]*2
In [2]: a
Out[2]: [[0, 0], [0, 0]]
In [3]: a[0][0]=1
In [4]: a
Out[4]: [[1, 0], [1, 0]]
Shouldn't be?
In [4]: a
Out[4]: [[1, 0], [0, 0]]
Create List of Single Item Repeated n Times in Python
As the link above explains, because you used the * operator, all the lists within your main list refer to the same list. So when you change one, you're really changing the others at the same time. Try this instead:
a = [[0, 0], [0, 0]]
a[0][0] = 1
That way you have explicitly defined two distinct sublists within a.
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 5 years ago.
I have an list like the following
line_37_data = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
When I print line_37_data[0][0] , the value 0 is printed.
When I update the list as line_37_data[0][0] = 5, the list gets modified like below
[[5, 0, 0], [5, 0, 0], [5, 0, 0]]
How can I can update the value in the list based on the index ?
Note :- I don't use NumPy. This is pure plain Python without any libraries. I am using 2.7 and not Python 3
If you pass in the same list as each element of your outer list, manipulating it will show in each place it appears. If you're just looking to fill a 2d list with zeros, list comprehension would be easy:
def generate_2d(h, w):
return [[0 for x in range(w)] for y in range(h)]
array = generate_2d(3, 3)
# Format is array[y][x] based on names in function
array[0][0] = 5
array[1][2] = 7
assert array == [
[5, 0, 0],
[0, 0, 7],
[0, 0, 0]]