Iterating across a filtered list

Arnaud Delobelle arnodel at googlemail.com
Tue Mar 13 22:16:32 CET 2007


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?

--
Arnaud




More information about the Python-list mailing list