-1

I have a 2d list of the form [[-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1]] and I want to change the list value at only a single index for example at (0,0) to 0 [[0, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1]].

I have a function that take the row index, col index and value and a 2d list called "picture " (plus other non-related arguments). and I try to change the picture in the row and column index to the value like this:

def create_picture(row, col, val, constraints_set, picture, possibilities, path):
    
    if (row, col, val) not in path:
        picture[row][col] = val
        path.add((row, col, val))

But for example for row=0, col=0 and val=0 (which I checked) and picture [[-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1]] I get: [[0, -1, -1, -1], [0, -1, -1, -1], [0, -1, -1, -1]] only after a single run.

When I run it manually using:

picture = [[-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1]]
picture[0][0] = 0
print(picture)

I do get [[0, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1]]

I hope you can help me, thanks.

usus1234
  • 1
  • 1
  • How did you create the value passed as `picture` in your call to `create_picture`? – Scott Hunter May 09 '22 at 12:49
  • What is `path` such that it has an `add` method? – Scott Hunter May 09 '22 at 12:51
  • I created the 2d picture by using ```picture = [[-1]*m]*n``` where m and n are the col and row dimenstion then called the function. Path is a set that contains tuples of (row, col, val) to see if I already put a value at that exact index. – usus1234 May 09 '22 at 12:55
  • @Guy in this call i used ```picture = [[-1]*4]*3``` – usus1234 May 09 '22 at 12:57
  • Does this answer your question [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly)? – Guy May 09 '22 at 12:57

0 Answers0