[Tutor] Testing for empty list

Todd Matsumoto tmatsumoto at gmx.net
Mon Oct 19 09:26:16 CEST 2009


The while loop will print each index of the list. In a way it checks that if the list is empty by printing the items. As far as I know there isn't any 'True' or 'False' output from a list.

If you want to do something if mylist is empty you can check it like this:

if not mylist:
   ... do something ...

T

-------- Original-Nachricht --------
> Datum: Sun, 18 Oct 2009 20:29:53 -0500
> Von: Wayne <srilyk at gmail.com>
> An: "tutor at python.org" <Tutor at python.org>
> Betreff: [Tutor] Testing for empty list

> 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
> 
> -- 
> 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

-- 
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/atbrowser


More information about the Tutor mailing list