In your first code, the outer i
loop:
world = []
for i in range(self.width):
world.append(None)
creates a list of None
:
world == [None, None, None, ...]
therefore in the inner j
loop, when you call
world[i].append(None)
you are trying to call:
None.append(None)
and you can't append to None
! The outer loop should have been:
world.append([]) # an empty list
Then the inner loop will work correctly with whatever (None
, ""
, etc.) you want to put in those lists.
It is worth noting that the second version is much neater than the first, with (obviously!) less potential for error. However, a small improvement (as you never use x
or y
):
self.map = [[None for _ in range(width)] for _ in range(height)]