Iterating across a filtered list

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Mar 13 22:48:38 CET 2007


En Tue, 13 Mar 2007 18:16:32 -0300, Arnaud Delobelle  
<arnodel at googlemail.com> escribió:

> On Mar 13, 8:59 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> wrote:
> [snip]
>> def find(self, search):
>>      search_re = re.compile(search, re.IGNORECASE)
>>      for result in [self.contacts[name] for name in self.contacts if
>> search_re.match(name)]:
>>          print result
>
> I do not see how
>
> for y in [f(x) for x in L if g(x)]:
>     do stuff with y
>
> can be preferable to
>
> for x in L:
>     if g(x):
>         do stuff with f(x)
>
> What can be the benefit of creating a list by comprehension for the
> sole purpose of iterating over it?

No benefit...

-- 
Gabriel Genellina




More information about the Python-list mailing list