0

Output not understood!

How and why is the output? https://code.sololearn.com/cvYjTW18i55q/?ref=app

27th Jun 2021, 2:52 PM
RuntimeERROR
RuntimeERROR - avatar
6 Answers
+ 7
because ([]) is same as [] (empty list == False) but [()] is True as not empty list (contains an empty tuple)... tuple containing a list must be written: ([],) to explicitly mark parenthesis as a tuple with only one item (else parenthesis are evaluated as mathematical expression ^^)
27th Jun 2021, 2:57 PM
visph
visph - avatar
+ 3
I believe it's because in: [([]), [()]] the value at the first index is: ([]) *** the parenthesis are treated as parenthesis NOT tuple SO, the correct first index value is: [] (an empty list) the second value is: [()] (a list w/ an empty tuple inside) When checking bools, anything 0, None, or empty iterables are considered False. Any value is True. So you get: False True
27th Jun 2021, 3:02 PM
Slick
Slick - avatar
+ 2
Slick not every time: don't believe that :D
27th Jun 2021, 3:04 PM
visph
visph - avatar
+ 2
For illustration you may output item and type: l=[([]),[()], (), (1), (1,), []] for i in l: print(i, type(i), bool(i), sep=" ")
27th Jun 2021, 3:09 PM
Lisa
Lisa - avatar
+ 1
visph dang bro, so much faster than me every time
27th Jun 2021, 3:03 PM
Slick
Slick - avatar
+ 1
RuntimeERROR in fact, parenthesis always are treated as mathematical expression... and tuple require to be parenthesis only in some cases: t = a, b create a tuple... also: t = a, but as list item, parenthesis are required to explicitly mark the tuple boundaries from list items boundaries (wich too use comma as separator) ;)
27th Jun 2021, 3:13 PM
visph
visph - avatar