empty clause of for loops

Sven R. Kunze srkunze at mail.de
Wed Mar 16 13:01:13 EDT 2016


On 16.03.2016 17:56, Sven R. Kunze wrote:
> 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

Time for a break. That is not going to work.

Will still think about why we don't use it (often/at all).

Best,
Sven



More information about the Python-list mailing list