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("")]
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("")]
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.
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