[Tutor] Empty list validation

W W srilyk at gmail.com
Mon Jun 9 16:54:42 CEST 2008


>>> def empty_or_not(mylist):
...     if mylist:
...         print "Not empty"
...     elif not mylist:
...         print "Empty!"
...
>>> empty_or_not([])
Empty!
>>> foo = []
>>> empty_or_not(foo)
Empty!
>>> foo.append('hi')
>>> empty_or_not(foo)
Not empty

My guess is that if any object is empty, it's considered "not".

HTH,
Wayne


On Mon, Jun 9, 2008 at 9:25 AM, qsqgeekyogdty at tiscali.co.uk
<qsqgeekyogdty at tiscali.co.uk> wrote:
> Hello
> Sorry if this is a basic question, but how do I check if list is empty
> or not and return True or False ;)
> Thanks
> David
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi


More information about the Tutor mailing list