Iterating across a filtered list

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Mar 14 05:28:38 EDT 2007


Arnaud Delobelle a écrit :
> 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;

I personnaly find it more readable. To me, it tells what, not how.

>    * it has no obvious performance benefit,

No.

>>> 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 :)

Don't worry, I still use them when appropriate.



More information about the Python-list mailing list