Iterating across a filtered list
Arnaud Delobelle
arnodel at googlemail.com
Tue Mar 13 16:44:27 EDT 2007
On Mar 13, 8:53 pm, Bruno Desthuilliers
<bdesth.quelquech... at free.quelquepart.fr> wrote:
> Paul Rubin a écrit :
[snip]
> > Iterators like that are a new Python feature
>
> List comps are not that new (2.0 or 2.1 ?):
> print "\n".join([contact for name, contact in contacts.items() \
> if search.match(name)])
You can write this, but:
* it is difficult to argue that it is more readable than Paul's (or
my) 'imperative' version;
* it has no obvious performance benefit, in fact it creates a list
unnecessarily (I know you could use a generator with recent python).
> > and they have
> > some annoying characteristics, like the way they mutate when you touch
> > them.
>
> While sequences are iterables, all iterables are not sequences. Know
> what you use, and you'll be fine.
...And know when to use for statements :)
--
Arnaud
More information about the Python-list
mailing list