I have a question about how to create a list within a list. For example, we use
[[0] * n for _ in range(m)]
to create a matrix with dimension n*m
. However, when I tried the similar thing to create a nested list.
l = [[(0, 1)]] * n
And then update the l[0]
, I find the l[1]
would also get updated. However, the approach l = [[(0, 1)] for _ in range(n)]
produces what I need.
Would you please help me understand this issue?
Thanks for your help.
Follow up: In the dynamic programming, we use [-math.inf]*n to construct a list and then update the values. Why this approach works, but the above approach failed? Thanks.