I try to generate a multi dimensional list depending on two variables: size and dim. Both of them are initialized with user input, so I don't know their values while coding. An example with dim = 3 and size = 2 would be: [[['+', '+'], ['+', '+']], [['+', '+'], ['+', '+']]]. What I have tried so far:
import copy
fields = '+'
for i in range(dim):
fields = [copy.copy(fields) for j in range(size)]
While this works totally fine for dim in [1, 2], it only creates references to the same list if dim > 2. Example with size = 3 and dim = 3:
>>> f
[[['+', '+', '+'], ['+', '+', '+'], ['+', '+', '+']], [['+', '+', '+'], ['+', '+', '+'], ['+', '+', '+']], [['+', '+', '+'], ['+', '+', '+'], ['+', '+', '+']]]
>>> f[0][0][0] = 'X'
>>> f
[[['X', '+', '+'], ['+', '+', '+'], ['+', '+', '+']], [['X', '+', '+'], ['+', '+', '+'], ['+', '+', '+']], [['X', '+', '+'], ['+', '+', '+'], ['+', '+', '+']]]
I know this behavior from code like f = [[['+'] * 3] * 3] * 3 and thought I could prevent it with copy.copy(), but this obviously doesn't work. I tried this with Python 3.2.2 on android (QPython 3) and with Python 2.7 on Windows, I got the same results. Please note that I don't want to use a non-standard library like numpy.