[Tutor] Empty list validation

C S Shyam Sundar csshyamsundar at gmail.com
Mon Jun 9 17:08:18 CEST 2008


One another simple method is to check its count using the *len *method.

eg:

if len(mylist) == 0:
     print "empty list"

This is particularly useful in situations where checking emptiness alone is
not enough.

On Mon, Jun 9, 2008 at 8:24 PM, W W <srilyk at gmail.com> wrote:

> >>> 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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
C S Shyam Sundar

[mobile] +91-97877-SHYAM | [voip] +1-425-998-1134

"It's easy to tell the difference between right and wrong. What's hard is
choosing the wrong that's more right."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080609/00a4271b/attachment.htm>


More information about the Tutor mailing list