Execute loop only once if condition is met

I was wondering if there is a pythonic way to write a code equivalent to the one below without defining doSomething as a separate function.

if test:
    for i in s:
        doSomething()
else:
    doSomething()

In particular, I am thinking of the use case where test checks if s exists.

Any suggestions?



Read more here: https://stackoverflow.com/questions/67391153/execute-loop-only-once-if-condition-is-met

Content Attribution

This content was originally published by toliveira at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: