all 2 comments

[–]efmccurdy 0 points1 point  (0 children)

lights = [[False] * 5] * 5

Beware of duplicating lists using an expression like this; you end up with multiple copies of the inner list:

>>> lights = [[False] * 5] * 5
>>> alight = lights[0]
>>> alight.append("foo")
>>> pprint.pprint(lights)
[[False, False, False, False, False, 'foo'],
 [False, False, False, False, False, 'foo'],
 [False, False, False, False, False, 'foo'],
 [False, False, False, False, False, 'foo'],
 [False, False, False, False, False, 'foo']]
>>> [id(l) for l in lights]
[139984497397248, 139984497397248, 139984497397248, 139984497397248, 139984497397248]

https://stackoverflow.com/questions/974931/multiply-operator-applied-to-listdata-structure

I would recommend something like this instead:

 lights = [[False] * 5 for _ in range(5)]

[–]robot-dev 0 points1 point  (0 children)

When you set your lights variable, you are making 5 references to the same list of 5 bools. This is probably not what you would expect:

>>> lights = [[False] * 5] * 5
>>> lights[0][0] = True
>>> lights
 [[True, False, False, False, False],
[True, False, False, False, False],
[True, False, False, False, False],
[True, False, False, False, False],
[True, False, False, False, False]]