I have a question concerning this piece of code:
def new_field():
global field
field= [[]]*9
for x in range(9):
for y in range(1,10):
field[x].append([y," "])
return field
The way I understand for-loops, it should return a list like this: [[[1, " "], [2, " "], [3, " "], ...[9, " "]], [1, " "], [2, " "], ...
which would mean that field should have a length of 9 for every index from 0 to 8 ->
len(field[0])
should be 9. Instead the length is 81 for every index, what should be the entire list can be found in every sublist.
It looks like this:
[[[1, ' '], [1, ' '], [1, ' '], [1, ' '], [1, ' '], [1, ' '], [1, ' '], [1, ' '], [1, ' '], [2, ' '], [2, ' '], [2, ' '], [2, ' '], [2, ' '], [2, ' '], [2, ' '], [2, ' '], [2, ' '], [3, ' '], [3, ' '], [3, ' '], [3, ' '], [3, ' '], [3, ' '], .......
Now I have a feeling that I simply dont understand how exactly for-loops work, but I just dont get what I am missing or where the problem in my thoughtprocess is.
Hope someone can help!