Search code examples
pythonpython-3.xbuilt-in

Behaviour of all() in python


>>all([])
True
>>all([[]])
False
>>all([[[]]])
True
>>all([[[[]]]])
True

The documentation of all() reads that it returns True is all the elements are True/For an empty list. Why does all([ [ ] ]) evaluate to False? Because [ ] is a member of [ [ ] ], it should evaluate to True as well.


Solution

  • >>all([])
    True
    

    because all iterables the list are True (there are zero iterables though)

    >>all([[]])
    False
    

    there is one empty iterable (innermost empty list) which would evaluate to False

    >>all([[[]]])
    True
    

    the only iterable here ( [[]] ) has one empty list inside of it, and hence evaluates to True

    >>all([[[[]]]])
    >>True
    

    same as above