empty clause of for loops

Sven R. Kunze srkunze at mail.de
Wed Mar 16 12:56:15 EDT 2016


On 16.03.2016 17:37, Random832 wrote:
> On Wed, Mar 16, 2016, at 11:17, Sven R. Kunze wrote:
>> I can imagine that. Could you describe the general use-case? From what I
>> know, "else" is executed when you don't "break" the loop. When is this
>> useful?
>
> for item in collection:
>     if good(item):
>        thing = item
>        break
> else:
>     thing = default # or raise an exception, etc

I was thinking about why we don't use it that often. My response to this 
example:

thing = item if item in collection else default



More information about the Python-list mailing list