+ 1
Can someone explain how this works?
my_list = [([]), [()]] for i in my_list: print(bool(i), end="") Output: FalseTrue
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)