Checking Null Lists

What is the most efficient way to check if a list contains only empty values ​​(not if the list is empty, but a list of empty elements)? I use the cool python implicit booleaness method in a for loop:

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

Anything better?

+5
source share
4 answers
if not any(lst):
    # ...

Must work. any()returns Trueif any element of the iterable that it passed evaluates True. Equivalent to:

def my_any(iterable):
    for i in iterable:
        if i:
            return True
    return False
+16
source
len([i for i in lst if i]) == 0
+3
source

Usage all:

   if all(item is not None for i in list):
      return True
    else:
      return False
+2
source
>>> l = ['', '', '', '']
>>> bool([_ for _ in l if _])
False
>>> l = ['', '', '', '', 1]
>>> bool([_ for _ in l if _])
True
+1
source

All Articles