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=['', '']
Using the built-in
any()