Single line if statement with a continue

dn PythonList at DancesWithMice.info
Thu Dec 15 00:24:15 EST 2022


On 15/12/2022 07.53, Aaron P wrote:
> I occasionally run across something like:
> 
> for idx, thing in enumerate(things):
>      if idx == 103:
>          continue
>      do_something_with(thing)
> 
> It seems more succinct and cleaner to use:
> 
> if idx == 103: continue.
> 
> Of course this would be considered an anti-pattern, and Flake8 will complain.
> 
> Any opinions, or feedback on the matter.


These aged-eyes prefer the second line and indentation.


However, another alternative (given simplicity of example):

for ...
     if idx != 103:
         do_something ...


Which could, in-turn, be boiled-down to a 'one-liner'.

-- 
Regards,
=dn


More information about the Python-list mailing list