0

I have no idea how this code works and it returns an output of the first element in the list.

I have tried with different inputs since though I'm getting all the same output.

[1,2,3][bool("")]
khelwood
  • 55,782
  • 14
  • 81
  • 108

2 Answers2

0

Since bool("")==False. Also, False is 0. Therefore [1,2,3][bool("")] is same as [1,2,3][0] which return first element of list ultimately.

Vineet Jain
  • 1,515
  • 4
  • 21
  • 31
0
bool("")==0

This is because "" is regarded as false. This means your statement will be,

[1,2,3][0]

which returns the first element of the list. In this case

1

Source: https://www.programiz.com/python-programming/methods/built-in/bool

Diaco
  • 241
  • 1
  • 3
  • 15