Think about a function that I'm calling for its side effects, not return values (like printing to screen, updating GUI, printing to a file, etc.).

def fun_with_side_effects(x):
    ...side effects...
    return y

Now, is it Pythonic to use list comprehensions to call this func:

[fun_with_side_effects(x) for x in y if (...conditions...)]

Note that I don't save the list anywhere

Or should I call this func like this:

for x in y:
    if (...conditions...):

Which is better and why?

Solution 1

It is very anti-Pythonic to do so, and any seasoned Pythonista will give you hell over it. The intermediate list is thrown away after it is created, and it could potentially be very, very large, and therefore expensive to create.

Solution 2

You shouldn't use a list comprehension, because as people have said that will build a large temporary list that you don't need. The following two methods are equivalent:

consume(side_effects(x) for x in xs)

for x in xs:

with the definition of consume from the itertools man page:

def consume(iterator, n=None):
    "Advance the iterator n-steps ahead. If n is none, consume entirely."
    # Use functions that consume iterators at C speed.
    if n is None:
        # feed the entire iterator into a zero-length deque
        collections.deque(iterator, maxlen=0)
        # advance to the empty slice starting at position n
        next(islice(iterator, n, n), None)

Of course, the latter is clearer and easier to understand.

Solution 3

List comprehensions are for creating lists. And unless you are actually creating a list, you should not use list comprehensions.

So I would got for the second option, just iterating over the list and then call the function when the conditions apply.

Solution 4

Second is better.

Think of the person who would need to understand your code. You can get bad karma easily with the first :)

You could go middle between the two by using filter(). Consider the example:

def func(x):
    print "call with %r"%x

for x in filter(lambda x: x>3, y):

Solution 5

Depends on your goal.

If you are trying to do some operation on each object in a list, the second approach should be adopted.

If you are trying to generate a list from another list, you may use list comprehension.

Explicit is better than implicit. Simple is better than complex. (Python Zen)

Solution 6

You can do

for z in (fun_with_side_effects(x) for x in y if (...conditions...)): pass

but it's not very pretty.

Solution 7

Using a list comprehension for its side effects is ugly, non-Pythonic, inefficient, and I wouldn't do it. I would use a for loop instead, because a for loop signals a procedural style in which side-effects are important.

But, if you absolutely insist on using a list comprehension for its side effects, you should avoid the inefficiency by using a generator expression instead. If you absolutely insist on this style, do one of these two:

any(fun_with_side_effects(x) and False for x in y if (...conditions...))


all(fun_with_side_effects(x) or True for x in y if (...conditions...))

These are generator expressions, and they do not generate a random list that gets tossed out. I think the all form is perhaps slightly more clear, though I think both of them are confusing and shouldn't be used.

I think this is ugly and I wouldn't actually do it in code. But if you insist on implementing your loops in this fashion, that's how I would do it.

I tend to feel that list comprehensions and their ilk should signal an attempt to use something at least faintly resembling a functional style. Putting things with side effects that break that assumption will cause people to have to read your code more carefully, and I think that's a bad thing.