I need an empty list with the following structure so I can later populate it without getting out of range errors:
[5[2[]]]
i.e.:
[[[], []], [[], []], [[], []], [[], []]]
So I tried declaring it this way:
[[[]]*n]*m
/ n=2
, m=5
But *
is copying references, so if i do something like this:
>>> foo = [[[]]*2]*5
>>> foo
[[[], []], [[], []], [[], []], [[], []]]
>>> foo[3][0] = 1
>>> foo
[[1, []], [1, []], [1, []], [1, []]]
The assigned value gets copied to all sub-lists with the same reference.
I can fix this by declaring foo
's sizes "statically" like:
foo = [[[], []], [[], []], [[], []], [[], []]]
But I need a parametric way like the previous example.
I would like to know if there's a parametric pythonic/compact way of of declaring and empty list of lists (like with n
and m
in the example), where all its elements have individual references.
Thanks in advance!