python counter-intuitive behaviors

Jimmy (xiaoke) Shen
1 min readDec 19, 2019

--

  1. nested list produced with multiplication is linked to the same list.

As discussed below, use “I have to ” to generate a nested list instead of the first one if you want to generate a grid.

In my view, this is strange and counter-intuitive behavior.  If I want to create a matrix-like set to None to start, instead of using:

mat = [[None] * N] * N

I have to use:

mat = [[None] * N for i in range(N)]

Detail can be found here:

https://bugs.python.org/issue27135

--

--

No responses yet