Python: Changing values of a nested list

JayB

So I'm using a nested list to store some data and I'm having trouble with changing specific values of one of the sublists:

if attributes[3] == 'W':
    self.board[3][3] = 'W'

(numbers are placeholders i'm using to test)

board is a class variable that is created as follows (I'm trying to create a grid as specified by a user, with their input for column and row sizes making up the first two parts of attributes)

self.board = []
rows = []
self.score = [0, 0]
for x in range(attributes[0]):
    rows.append('')
for y in range(attributes[1]):
    self.board.append(rows)

However, whenever I try to change the value of a sublist, it changes the values for all sublists for that same index:

[['', '', '', 'W', '', '', '', ''], ['', '', '', 'W', '', '', '', ''], ['', '', '', 'W', '', '', '', ''], ['', '', '', 'W', '', '', '', ''], ['', '', '', 'W', '', '', '', ''], ['', '', '', 'W', '', '', '', ''], ['', '', '', 'W', '', '', '', ''], ['', '', '', 'W', '', '', '', '']]

I can't figure out what's wrong. Anyone have any ideas?

Marcin

I think the reason for this can be demonstrated on this very simple example:

a = [1,2,3]

b = [a,a,a] # <- this is what you are actually doing

print(list(map(id,b)))
#[140475353832712, 140475353832712, 140475353832712]

Please not that all ids are same above. This means that you are stroning in your list, references to the same object. thus changing one subsist, changes every sublist.

However, you should be doing this (or some equivalent):

b2 = [a[:], a[:], a[:]] #
print(list(map(id,b2)))
#[140475353832136, 140475353832072, 140475353832520]

Please note, different id for each sublist. Now b2 contains references to different sublists.

To sum up, you should have this:

for y in range(attributes[1]):
    self.board.append(rows[:])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Changing filtered values in a nested list

Python list values not changing

Recursively changing values of a nested dataclass in python

Changing list of list values in place in python

Multiple values change when changing a single value in a nested list

Changing the Duplicate values in the multi dimensional list in Python

Changing nested object values

Append values to a list that has a nested list python

Inner values in Python nested dictionary changing for no clear reason

Python pandas sort_values() with nested list

Nested Dictionary Comprehension in Python with List of Values

Finding nested list with identical values in Dictionaries [Python]

Python Group Values in a Nested List At Corresponding Indexes

PYTHON: Finding the average of values of a nested list

Create a list of values from nested dictionary Python

Python: Dividing values of nested list with values with values of dictionary

Changing reference values in a List

Changing values on a list of lists

Changing values of a list of namedtuples

Changing multiple values in a list

Ref on a list not changing values

Changing values of list in multiprocessing

Changing a list of values in Awk

Changing values in nested JS arrays

Changing specific entry in nested list

changing a float inside a nested list

Python searching in nested lists to find values and insert values into a new list

Is there any python function to check for nan values in list comprehension without changing it

Python rearrange a list without changing the values and making every rearrangment different