-1

I understand the below list concatenation:

r = [1, 2] + [False]
# Output: r = [1, 2, False]

However, I am not able to understand the below syntax:

r = [1, 2][False]
# Output: 1
Michael M.
  • 10,486
  • 9
  • 18
  • 34
meallhour
  • 13,921
  • 21
  • 60
  • 117
  • Maybe dupe: [Accessing elements from a Python list using Boolean indexing](https://stackoverflow.com/q/49633222/674039) – wim Sep 14 '22 at 01:27

1 Answers1

3

You're defining an unnamed list, then indexing it with False. Because False has a numeric value of zero (bool is actually a subclass of int, where False has a value 0, and True has a value 1), that's equivalent to indexing it as index 0, so it's like you wrote:

__unnamed = [1, 2]
r = __unnamed[0]
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271