2

Is there a difference in intialising a list using this

l = [0] * 20

compared to this

l = [0 for i in range(20)]

Under the hood, how does [0] * 20 work compared to list comprehension in Python? The former method seems to result in faster runtime in LeetCode

ZooPanda
  • 331
  • 3
  • 11
  • 1
    For mutable objects, there is a big difference between them: [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – Mechanic Pig Oct 19 '22 at 04:41
  • Ah, this answers it, thanks! – ZooPanda Oct 19 '22 at 04:50

0 Answers0