[Tutor] Testing for empty list

Dave Angel davea at ieee.org
Mon Oct 19 10:52:40 CEST 2009



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


More information about the Tutor mailing list