[Tutor] Testing for empty list

Todd Matsumoto tmatsumoto at gmx.net
Mon Oct 19 11:24:48 CEST 2009


I don't understand how the while loop efficiently tests if the list is empty. Why would going through the entire list be a good test to simply see find out if the list is empty or not.

Wouldn't you want to test the list itself, rather than the contents of it?

Cheers,

T

-------- Original-Nachricht --------
> Datum: Mon, 19 Oct 2009 04:52:40 -0400
> Von: Dave Angel <davea at ieee.org>
> An: Wayne <srilyk at gmail.com>
> CC: "tutor at python.org" <Tutor at python.org>
> Betreff: Re: [Tutor] Testing for empty list

> 
> 
> Wayne wrote:
> > Hi, I think I recall seeing this here, but I wanted to make sure I'm
> > correct.
> > Is the best way to test for an empty list just test for the truth value?
> > I.e.
> >
> > mylist = [1,2,3]
> >
> > while mylist:
> >    print mylist.pop()
> >
> > Thanks,
> > Wayne
> >
> >   
> My take is simple:  Use the above form if you *know* that mylist is in 
> fact a list.  If you don't know its type for sure (the name is a clue, 
> but not conclusive ;-) ) then use a more specific test.
> 
> In your case, you know it's a list, or least something that supports 
> pop().  So your form should be great.
> 
> DaveA
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01


More information about the Tutor mailing list