python how to check list does't contain any value

python, python-2.7, python-3.x

Solution

Using the built-in `any()`

def foo(l=[]):
    if any(l):
        print 'List is not empty'
    else:
        print 'List is empty'

foo([''])
# List is empty

Problem

consider this simple function ``` def foo(l=[]): if not l: print "List is empty" else : print "List is not empty" ``` Now let's call foo ``` x=[] foo(x) #List is empty foo('') #List is empty ``` But if x=[''] the list is not considered as empty!!! ``` x=[''] foo(x) #List is not empty ``` Questions - Why list of empty values are not considered as empty? (In case of variable it is considered as empty e.g.) ``` x='' if x:print 'not empty!!' else: print 'empty' ``` How can I modify function foo() so that list will be considered as empty in all these cases: `x=[]` , `x=['']`, `x=['', '']`

Original source