for ... else ?

Gabriel Genellina gagsl-py2 at
Tue Jun 12 20:55:36 CEST 2007

En Tue, 12 Jun 2007 10:41:28 -0300, Chris Mellon <arkanes at>  

> On 6/12/07, Gabriel Genellina <gagsl-py2 at> wrote:
>> for x in iterable:
>>    do something with x
>> else:
>>    do something when there are no more x
>> You can think the above as:
>> while there are still values in iterable:
>>    do something with the next value
>> else:
>>    do something when there are no more items
> This is a good way of phrasing it and I hope I can remember it,
> because for..else always gives me trouble. To me, "else" indicates the
> negative condition and I intuitively associate it with executing the
> loop early, not normal exit. Personally, I think a different keyword
> (maybe "after"?) would have done a better job of clarifying this.

Yes, maybe, but it's hard to find a keyword equally applicable to "for"  
and "while" and creating two new keywords for essencially the same thing  
would be too much... Anyway it's too late to be changed now.

Gabriel Genellina

More information about the Python-list mailing list