-3

I try to change the first value of a field in python to true. What am I doing wrong? The whole column changes and I don't understand why.

My Code:

def selectElement(self, col, row):
    print(col, row)
    print(self)
    self.field[col][row] = True
    print(self)

def __str__(self):
    out = '\n'
    for col in self.field:
        for value in col:
            out += str(value) + ' '
        out += '\n'
    return out

The Output:

0 0

False False False
False False False
False False False


True False False
True False False
True False False

The list is initialised with:

self.list = [[False] * size] * size
lumpigerAffe
  • 111
  • 1
  • 8
  • 3
    Show us your `__init__` method. This is likely a duplicate of [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – Patrick Haugh Nov 06 '18 at 15:18
  • Hello and welcome to SO. It's impossible to answer your question without at least the definition of `self.field`. Please post a proper MCVE (https://stackoverflow.com/help/mcve). – bruno desthuilliers Nov 06 '18 at 15:19
  • I created it with: [[False] * size] * size – lumpigerAffe Nov 06 '18 at 15:22

1 Answers1

0

I fixed it with:

self.field = [[False] * size for n in range(size)]
lumpigerAffe
  • 111
  • 1
  • 8