StopIteration in the if clause of a generator expression

Raymond Hettinger vze4rx4y at verizon.net
Tue Apr 5 09:13:21 EDT 2005


> > [Steven Bethard]
> > > So do I read this right in preferring
> > >      [<x> for <y> in <z>]
> > > over
> > >      list(<x> for <y> in <z>)

[Raymond Hettinger]
> > Yes!

[Simon Brunning]
> Why? (Serious question. I'm sure that you have a good reason - I just
> can't figure out what it is.)
>
> The generator expression has the advantage of not leaking references
> into the enclosing namespace. What's advantage of the list comp?

One advantage relates to mental parsing and chunking.
A list comp reads as a single step:  "make a list".
The genexp form reads as "make a generator and turn it into a list."

The listcomp form has slightly more economy of expression (it is succinct) and
the brackets are a nice visual cue that may save a neuron or two.

Another advantage is that listcomps are older.   They tend to be better
understood already.  And, they run on older pythons.

The design rule, "use listcomps to make lists and genexps to make generators",
encourages data centric thinking.With being distracting, it helps maintain an
awareness of whether you're filling memory or
generating elements one-at-a-time.

Internally, there are performance differences favoring listcomps when the
desired output is a list.  List comprehensions are interpreted immediately
through syntax rather than a global lookup of the list() builtin.  The code for
listcomps does not have to create, switch between, and destroy a separate
stackframe.  The compiler creates custom code for list comps that takes
advantage of the new LIST_APPEND opcode.

Partially balancing out all of the above are some small advantages for the
list(somegen) form.  It lets you forget about listcomps and it works with other
contructors, deque(somegen) or set(somegen) for example.

To my tastes, the net balance favors using listcomps whenever you need to create
a list.


Raymond Hettinger







More information about the Python-list mailing list