Case-insensitive string compare?

Maric Michaud maric at
Fri Sep 5 13:16:40 CEST 2008

Le Friday 05 September 2008 08:30:44 Fredrik Lundh, vous avez écrit :
> Maric Michaud wrote:
>  > You''ll often see for loops written like this :
> >
> > for i in (e for e in iterable if predicate(e)) :
> >     ...
> luckily, I don't.  most people, when faced with that problem, writes it
> in the obvious way:
>     for i in iterable:
>         if predicate(i):
>             ...

So do I, most often, but this construct is common, I think because it makes 
clear what the for loop is iterating over, also it comes naturally once used 
to the more elaborated 

for i in (expr(e) for e in iterable if predicate(e)) :

This one is truly a gain for readability IMO, compared to :

     for i in iterable:
         i = expr(i)
         if predicate(i):

In the latter, the reader need more effort to figure out what finally "i" is 
in the loop.

> to avoid an extra iterator and save typing.

and at the cost of an extra indentation level.

> </F>
> --


Maric Michaud

More information about the Python-list mailing list