mardi 3 mars 2020

Calling functions in a for loop in one line if a condition is met

I would like to call a function, ignoring its output, in one line if a condition is met. My expected syntax is this:

f(i) if condition else continue for i in range(rng)

I can do it within a list like this:

[f(i) if condition else None for i in range(rng)]

But here I create a list although I do not need one. I know that Python is not the most suitable language to fine-tune performance and that this has nearly no effect, but it bothers me. Moreover, I think it makes the code look like the function has an output, or that the output is used.

So, is there a way to call a function if a condition is met and do nothing if not, in a for loop and in one line, without doing anything else like creating an unneeded list?

Aucun commentaire:

Enregistrer un commentaire