1

I want to create a pre-sized multi-dimensional list. I have as an experiment:

>>>md = [[0]*5]*5
>>>md[1][1] = 6
>>>md
[[0, 6, 0, 0, 0], [0, 6, 0, 0, 0], [0, 6, 0, 0, 0], [0, 6, 0, 0, 0], [0, 6, 0, 0, 0]]

which I don't expect.

then:

>>> a = [[0] * 5 for i in range(5)]
>>> a[1][1] = 6
>>> a
[[0, 0, 0, 0, 0], [0, 6, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

which is what I expect. what is going on here?

mcgyver5
  • 1,075
  • 2
  • 13
  • 28

0 Answers0