Hi. So I'm trying to generate a nested list in Python based on a width and a height. This is what I have so far:
width = 4
height = 5
row = [None]*width
map = [row]*height
Now, this obviously isn't quite right. When printed it looks fine:
[[None, None, None, None],
[None, None, None, None],
[None, None, None, None],
[None, None, None, None],
[None, None, None, None]]
But attempting to assign a value to a position like so:
map[2][3] = 'foo'
I get:
[[None, None, None, 'foo'],
[None, None, None, 'foo'],
[None, None, None, 'foo'],
[None, None, None, 'foo'],
[None, None, None, 'foo']]
Clearly this happens because each sublist is really just referencing the same object, row, so changing one, changes them all. So this is closest I've got!
How can I dynamically generate a nested list? Thanks!