0
import random
n=5
list = [[None]]*n
for i in list:
    i[0] = random.uniform(0,1)

print(list)

No matter the value of n, all list entries seem to always end up getting the same value. Why?

(For context, i was trying to fill a matrix with random numbers)

  • This question should provide some help - https://stackoverflow.com/questions/3009091/more-elegant-way-to-initialize-list-of-duplicated-items-in-python – Mortz Nov 02 '21 at 15:02
  • In short when you do a `lst = [[None]]*5` - you are creating 5 copies of the same `[None]` list. – Mortz Nov 02 '21 at 15:03

0 Answers0