Search code examples
pythonempty-list

Checking for lists of empty values


What is the most effective way to check if a list contains only empty values (not if a list is empty, but a list of empty elements)? I am using the famously pythonic implicit booleaness method in a for loop:

def checkEmpty(lst):
    for element in lst:
        if element:
            return False
            break
    else:
        return True

Anything better around?


Solution

  • if not any(lst):
        # ...
    

    Should work. any() returns True if any element of the iterable it is passed evaluates True. Equivalent to:

    def my_any(iterable):
        for i in iterable:
            if i:
                return True
        return False