Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 595
Kracit Avatar asked Aug 30 '12 15:08

Kracit


2 Answers

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
like image 120
Silas Ray Avatar answered Sep 22 '22 15:09

Silas Ray


len([i for i in lst if i]) == 0
like image 36
João Silva Avatar answered Sep 20 '22 15:09

João Silva