-1

What's the difference between [[False] * 26] * 26] and [[False] * 26 for _ in range(0, 26)] in Python ?

zynl
  • 47
  • 2
  • 6

1 Answers1

1

It's all about aliasing. It becomes apparent when you modify the inner list.

>>> a = [[False] * 26] * 26
>>> a[0][0] = True
>>> a[1][0]
True
>>> b = [[False] * 26 for _ in range(0, 26)]
>>> b[0][0] = True
>>> b[1][0]
False
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415