I would post this in my other thread, but I marked that as solved so I think less people would look at it. I want to say this:
if list1 is empty:
do this
elif list1 has any value in it:
do this
How do I word that in python?
I would post this in my other thread, but I marked that as solved so I think less people would look at it. I want to say this:
if list1 is empty:
do this
elif list1 has any value in it:
do this
How do I word that in python?
I would post this in my other thread, but I marked that as solved so I think less people would look at it. I want to say this:
if list1 is empty: do this elif list1 has any value in it: do this
How do I word that in python?
>>> a = []
>>> if a:
... print "not empty"
...
>>> b = ['a']
>>> if b:
... print "not empty"
...
not empty
>>>
or you can use len()
or for the literal match,
if not list: # condition will be True if 'list' is any of 0, [], "", (), etc.
do(this)
else:
do(that)
This might be easier to understand ...
# test if list is empty
list1 = []
list2 = [7]
if len(list1) > 0:
print "list1 is not empty"
else:
print "list1 is empty"
if len(list2) > 0:
print "list2 is not empty"
else:
print "list2 is empty"
"""
output =
list1 is empty
list2 is not empty
"""
Thanks guys this helped alot.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.