+ 1

Can someone explain how this works?

my_list = [([]), [()]] for i in my_list: print(bool(i), end="") Output: FalseTrue

2nd Oct 2019, 9:09 AM
ibrocodes
ibrocodes - avatar
1 Answer
+ 3
The parentheses in ([]) just reorders the expression which in the end is just the same thing as [] (Note that it wouldn't be the same if it was ([],) an empty list in a tuple) [()] is an empty tuple in a list When converting any container to a bool, an empty container is False, otherwise it is True my_list is[ [], [()] ] so the for loop iterates twice bool( [] ) == False bool( [()] ) == True (because the list is not actually empty; it contains an empty tuple)
2nd Oct 2019, 9:21 AM
jtrh
jtrh - avatar