+ 1

Boolean login in Python on lists and tuples

Hi, why is the output of this code: False True and not False False? my_list = [([]),[()]] for i in my_list: print (bool(i), end=" ")

20th Jul 2019, 4:20 PM
Marek Kluczewski
Marek Kluczewski - avatar
1 Resposta
+ 9
The trick here is, [] is an empty list but [()] isn't and parentheses"( )" around any entity is neglected until it contains a comma ", ", in which case it'd be a tuple. In short: ([]) is [], length 0 hence False and [()], length 1, hence True.
20th Jul 2019, 4:34 PM
Шащи Ранжан
Шащи Ранжан - avatar