views:

2141

answers:

5

When should you use generator expressions vs. list comprehensions in Python and vice-versa?

# Generator expression
(x*2 for x in range(256))

# List comprehension
[x*2 for x in range(256)]
+11  A: 

Use list comprehensions when the result needs to be iterated over multiple times, or where speed is paramount. Use generator expressions where the range is large or infinite.

John Millikin
+21  A: 

Iterating over the generator expression or the list comprehension will do the same thing. However, the list comp will create the entire list in memory first while the generator expression will create the items on the fly, so you are able to use it for very large (and also infinite!) sequences.

dF
+25  A: 

John's answer is good (that list comprehensions are better when you want to iterate over something multiple times). However, it's also worth noting that you should use a list if you want to use any of the list methods. For example, the following code won't work:

def gen():
    return (something for something in get_some_stuff())

print gen()[:2]     # generators don't support indexing or slicing
print [5,6] + gen() # generators can't be added to lists

Basically, use a generator expression if all you're doing is iterating once. If you want to store and use the generated results, then you're probably better off with a list comprehension.

Since performance is the most common reason to choose one over the other, my advice is to not worry about it and just pick one; if you find that your program is running too slowly, then and only then should you go back and worry about tuning your code.

Eli Courtwright
Sometimes you *have* to use generators -- for example, if you're writing coroutines with cooperative scheduling using yield. But if you're doing that, you're probably not asking this question ;)
ephemient
+1  A: 

Sometimes you can get away with the tee function from itertools, it returns multiple iterators for the same generator that can be used independently.

Jacob Rigby
+3  A: 

The benefit of a generator expression is that it uses less memory since it doesn't build the whole list at once. Generator expressions are best used when the list is an intermediary, such as summing the results, or creating a dict out of the results.

For example:

sum( (x*2 for x in xrange(256)) )

dict( ((k, some_func(k) for k in some_list_of_keys) )

The advantage there is that the list isn't completely generated, and thus little memory is used (and should also be faster)

You should, though, use list comprehensions when the desired final product is a list. You are not going to save any memeory using generator expressions, since you want the generated list. You also get the benefit of being able to use any of the list functions like sorted or reversed.

For example:

reversed( [x*2 for x in xrange(256] )

Chuck
There is a hint put for you right in the language that generator expressions are meant to be used that way. Lose the brackets! `sum(x*2 for x in xrange(256))`
kaizer.se