I have a list which is full of None
. How to replace only one element without affecting the others. I understand that this is due to python shallow copying:
>>> a = [[None] * 3] * 3
>>> a
[[None, None, None], [None, None, None], [None, None, None]]
>>> a[0][1] = 2
>>> a
[[None, 2, None], [None, 2, None], [None, 2, None]]
I would like:
>>>a[0][1] = 2
[[None, 2, None], [None, None, None], [None, None, None]]
I know this can be done using a list comprehension like:
>>> a = [[None] * 3 for _ in range(3)]
>>> a
[[None, None, None], [None, None, None], [None, None, None]]
>>> a[0][1] = 2
>>> a
[[None, 2, None], [None, None, None], [None, None, None]]
But I was wondering if there was another way around? Something more pythonic?
I need this because bokeh
's grid
does not seem to accept anything else than a None
in order not to have a graph. If you know a better alternative. (In this case a[0][1]
would be replaced by a plot).