This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 3 months ago.
I created a 4x5 2D array using python, and when I wanted to change a number inside it, it automatically changes the number in every row
rows,cols = (4,5)
arr = [[0]*cols]*rows
print (arr)
And this is how the output shows
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
After I created the array, I decide to change a number in the first row
arr[0][2] = 3
print(arr)
But it appears like this
[[0, 0, 3, 0, 0], [0, 0, 3, 0, 0], [0, 0, 3, 0, 0], [0, 0, 3, 0, 0]]
I checked with it and I still can't find any problem in it. May someone help me with it?
'Multiplying' the list copies the value references repeatedly - which is fine for primitives but not so much for lists, like you've seen. If you want different instances of the list, you could use list comprehension:
rows, cols = (4, 5)
arr = [[0] * cols for _ in range(rows)]
arr[0][2] = 3
print(arr) # [[0, 0, 3, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
Related
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 1 year ago.
I'm guessing the problem happens because I append the same array multiple times and any changes made after are made to all the nested arrays. Is there a way to add them to an array and not get that to happen. For what I'm doing I to need about 500 different nested arrays, so doing it manually is a pain.
Output:
Before append
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
after append
[[0, 2, 0, 0], [0, 2, 0, 0], [0, 2, 0, 0], [0, 2, 0, 0]]
What I expected to happen after append:
[[0, 0, 0, 0], [0, 2, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Code:
testArray = []
nestedTestArray = []
def testarraynestedcreate():
for countforloop1 in range(4):
nestedTestArray.append(0)
testArray.append(nestedTestArray)
testarraynestedcreate()
print(testArray) # before change
testArray[1][1] = 2 # change element in array
print(testArray) # after change )
You can use copy():
testArray.append(nestedTestArray.copy())
This way each list is independent from each other (and not a reference to the first one).
This question already has an answer here:
Strange behavior of lists in python [duplicate]
(1 answer)
Closed 2 years ago.
In this game I'm making, 3's are blocks that people can't move over and 4's are traps. Traps are meant to be hidden but whenever I hide them with the Row variable, it changes the actual Board variable
I took the bad code out of my whole program and it looks like this:
Board = [[1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [3, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 4, 0], [0, 0, 4, 0, 0, 0, 0, 4], [0, 0, 3, 0, 4, 0, 0, 0], [0, 0, 3, 3, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2]]
for Loop in range(len(Board)):
Row = Board[Loop]
for Loop2 in range(len(Row)):
if Row[Loop2] == 4:
Row[Loop2] = 0
print(Row)
print(Board)
Whenever the number 4 is replaced with a 0, it gets replaced with a 0. This is only meant to happen to the variable Row, but also happens to Board for some reason. Any reason why?
Row = Board[Loop]
Row points to the same memory location, that's why changes in it are also copied to Board. Use Row = Board[Loop][:] to create a copy
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 9 years ago.
I have a list of List say mysolution:
>>>mySolution
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> mySolution[0][0] = 1
>>> mySolution
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
Intended output:
[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
why is it that all the 1st elements in my list of list's is being changed to 1?
I would only like to change the first element of the first list to 1.
What matters is how you created your original mysolution list. As it seems, it contains four times the same list which is why changing it once will make it change in all four locations.
To initialize independent zero-filled lists like that, you can do the following:
mysolution = [[0] * 4 for i in range(4)]
It's quite possible that you created the list like this:
mySolution = [0]*4
mySolution = [mySolution]*4
Or equivalently:
mySolution = [[0]*4]*4
Either of the above snippets will create a list with four sublists which are copies of the exact, same sublist, so any modification on one sublist will be reflected on the others - they're one and the same. The solution is to create four different sublists:
mySolution = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Or a bit shorter:
mySolution = [[0]*4 for _ in xrange(4)]
Because all the contained lists are actually the same list. When you do:
l = [0, 0, 0, 0]
my_solution = [l, l, l]
Then, my_solution[0], my_solution[1], and my_solution[2] are references to the same object (l).
If you modify the list in one location, it changes everywhere. That is because lists are mutable objects.
Instead, use multiple lists:
l1 = [0, 0, 0, 0]
l2 = [0, 0, 0, 0]
l3 = [0, 0, 0, 0]
my_solution = [l1, l2, l3]
Which will work as intended.
please note that this is doing fine:
mySolution = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
mySolution[0][0] = 1
print mySolution
>>>
[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
it all depends on how you initialized your solution. this
mySolution = [[0, 0, 0, 0]]*4
mySolution[0][0] = 1
print mySolution
gives
>>>
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
>>>
because here each array [0, 0, 0, 0] in mySolution is a copy of initialization array [0, 0, 0, 0] in [[0, 0, 0, 0]]*4. if you change first element of first array, copy of it also change.
with this initialization mySolution = [[0, 0, 0, 0] for x in range(4)] you are not copying the array but appending [0,0,0,0] four times, giving the result that you are expecting.
I'm trying to print a matrix with user assigned variable for the length and width, but have used manual assignment for easier reading. Right now my output doesn't include any new lines, but that is what I'm attempting to do.
def matrix(rows,cols):
grid = [[0 for i in range(cols)] for i in range(rows)]
return grid
rows = 5
cols = 5
print(matrix(rows,cols))
Is it possible to insert a print("\n") statement into the for statement to properly print out the matrix. Currently the output is as follows:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
Desired output:
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]
That happens to be the exact behaviour of pprint.
from pprint import pprint
def matrix(rows,cols):
grid = [[0 for i in range(cols)] for i in range(rows)]
return grid
rows = 5
cols = 5
pprint(matrix(rows,cols))
pprint doing what you need is nice, but shouldn't you be using a class for your matrix type? Using a naked list of lists may bite you later.
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 9 years ago.
I have a list of List say mysolution:
>>>mySolution
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> mySolution[0][0] = 1
>>> mySolution
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
Intended output:
[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
why is it that all the 1st elements in my list of list's is being changed to 1?
I would only like to change the first element of the first list to 1.
What matters is how you created your original mysolution list. As it seems, it contains four times the same list which is why changing it once will make it change in all four locations.
To initialize independent zero-filled lists like that, you can do the following:
mysolution = [[0] * 4 for i in range(4)]
It's quite possible that you created the list like this:
mySolution = [0]*4
mySolution = [mySolution]*4
Or equivalently:
mySolution = [[0]*4]*4
Either of the above snippets will create a list with four sublists which are copies of the exact, same sublist, so any modification on one sublist will be reflected on the others - they're one and the same. The solution is to create four different sublists:
mySolution = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Or a bit shorter:
mySolution = [[0]*4 for _ in xrange(4)]
Because all the contained lists are actually the same list. When you do:
l = [0, 0, 0, 0]
my_solution = [l, l, l]
Then, my_solution[0], my_solution[1], and my_solution[2] are references to the same object (l).
If you modify the list in one location, it changes everywhere. That is because lists are mutable objects.
Instead, use multiple lists:
l1 = [0, 0, 0, 0]
l2 = [0, 0, 0, 0]
l3 = [0, 0, 0, 0]
my_solution = [l1, l2, l3]
Which will work as intended.
please note that this is doing fine:
mySolution = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
mySolution[0][0] = 1
print mySolution
>>>
[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
it all depends on how you initialized your solution. this
mySolution = [[0, 0, 0, 0]]*4
mySolution[0][0] = 1
print mySolution
gives
>>>
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
>>>
because here each array [0, 0, 0, 0] in mySolution is a copy of initialization array [0, 0, 0, 0] in [[0, 0, 0, 0]]*4. if you change first element of first array, copy of it also change.
with this initialization mySolution = [[0, 0, 0, 0] for x in range(4)] you are not copying the array but appending [0,0,0,0] four times, giving the result that you are expecting.