What is the best way to skip entries in a generator expression that are created using a function and another generator?
In essence, I am looking for an expression that looks like this:
some_iter = (some_function(_x, _y, **kwargs) or continue for _x, _y in some_generator(*x))
(but the continue statement obviously doesn't work in this context)
Functionally, it should act like this:
def some_iter(*x):
for _x, _y in some_generator(*x):
x = some_function(_x, _y, **kwargs)
if x:
yield x
A list comprehension allows to filter and then map. You want to manually
mapyour function first.The above is for a
generatorthat yields single arguments. You can useitertools.starmapif it returns atupleof arguments.Finally, if you also need to pass in keyword arguments, you will need to rely on a
lambdafunction.Although, note that at that point, the function-style generator might be more readable.