1

After multiple testing I have realized that

a = 1000
b = 2000
test = [ [ 0 for i in range(a) ] for j in range(b) ]

takes more time as compared to

a = 1000
b = 2000
test = [ [0] * a for i in range(b) ]

in python3. Anyone can explain why is that? Or am I missing something here?

  • not sure but could be related to this : https://stackoverflow.com/questions/37706477/why-is-list-multiplication-so-fast -> It should be noted that these two construction methods are not equivalent, particularly when the value inside the list is mutable. For example, [object()] * 10 will give you a list of 10 of the same object, while [object() for _ in range(10)] will give you a list of 10 distinct objects. – pippo1980 Sep 21 '22 at 18:39
  • I think yeah, in a way it answers my question, thanks @pippo1980 – Kartik Hakim Sep 21 '22 at 21:06

0 Answers0