empty clause of for loops

Peter Otten __peter__ at web.de
Wed Mar 16 11:53:51 EDT 2016


alister wrote:

> On Wed, 16 Mar 2016 11:47:31 +0100, Peter Otten wrote:
 

>> I'm kidding, of course. Keep it simple and use a flag like you would in
>> any other language:
>> 
>> empty = True:
>> for item in items:
>>     empty = False ...
>> if empty:
>>     ...
> 
> or even use the loop variable as the flag
> 
> item=None
> for item in items:
>         #do stuff
  if item is None:
>         #do something else

I like that better now I see it. I've always used

i = -1
for i, v in enumerate(...):
   ...
if i != -1:
   ...

which led me to think that enumerate() was necessary for the idiom.





More information about the Python-list mailing list