Possible Duplicate:
Is it Pythonic to use list comprehensions for just side effects?
proper use of list comprehensions - python
Python has the useful and elegant list comprehension syntax. However AFAIK it always produces a list. Sometimes I feel the urge to use list comprehension just for its compactness and elegance without needing the resulting list:
[some_func(x) for x in some_list if x>5]
some_func()
may return something which I don't need, it may not return anything at all. I tried the generator syntax:
(some_func(x) for x in some_list if x>5)
but as you may guess, it doesn't iterate over some_list
. It does so only within certain context:
other_func(some_func(x) for x in some_list if x>5)
So... is there a syntax I'm missing to get this to work, or should I always fall back to 3 lines?
for x in some_list:
if x>5:
some_func(x)