[Tutor] Testing for empty list

शंतनू shantanoo at gmail.com
Mon Oct 19 08:28:35 CEST 2009


On 19-Oct-09, at 7:11 AM, vince spicer wrote:

>
>
> On Sun, Oct 18, 2009 at 7:29 PM, Wayne <srilyk at gmail.com> 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
>
>
> I believe it is better to check the list length, I think some  
> changes are coming on evaling list
>
>
> mylist = [1,2,3]
>
> while len(mylist) > 0:
>    print mylist.pop()

or
<code>
while mylist != []:
     print mylist.pop()
</code>

or for reverse iteration without popping the values in the list.

<code>
for x in reversed(mylist):
     print x
</code>

regards,
shantanoo


More information about the Tutor mailing list