+ 4

Can you explain this output?

x=([]) print(bool(x)) #output=False,but why... y=[()] print(bool(y)) #output=Ture,but why.... can anyone explain this.

14th May 2018, 4:16 PM
Maninder $ingh
Maninder $ingh - avatar
5 Réponses
+ 5
The first, x, is an empty list. The second list, y, is not empty. print(bool(([],)) will return true as it is a tuple containing an empty list.
14th May 2018, 4:28 PM
davy hermans
davy hermans - avatar
+ 3
Good question! [()] is not empty because it is a list containing a tuple. But, ([]) becomes just [] because python thinks there are just extra parentheses. However, if you try putting a comma after the list like so: ([],) or just [], Suddenly it becomes a tuple containing an empty list. Hope this helped :)
15th May 2018, 5:57 PM
Just A Rather Ridiculously Long Username
- 1
mmm
19th May 2018, 4:18 PM
Ajay
Ajay - avatar
- 2
hhhhiolhouv ytiv
14th May 2018, 8:33 PM
GAMEKILLER
- 3
hj
14th May 2018, 8:32 PM
GAMEKILLER