Python style.

Jacek Generowicz jmg at ecs.soton.ac.uk
Wed May 10 04:49:18 EDT 2000


Fredrik Lundh wrote:

> Jacek Generowicz <jmg at ecs.soton.ac.uk> wrote:
> > How would you rewrite the following code, in good
> > python style ?
> >
> > list1 = [ 1,2,3,4,5,6 ]
> > list2 = [ 6,5,4,3,2,1 ]
> >
> > count = 0
> > for item in list1:
> >     print item - list2[count]
> >     count = count + 1
>
> slightly less obscure:
>
>     for i in range(len(list1)):
>         item1 = list1[i]
>         item2 = list2[i]
>         ...

Yep, had this one too.

> the slightly more obscure way:
>
>     for item1, item2 in map(None, list1, list2):
>         ...

This is more like what I wanted. Well, I wanted
something like:
for item1, item2 in list1, list2:

Maybe the map version is less legible than the range
version though . . .

> note that future Python versions may also support
> something like:
>
>     for item1 in list1; item2 in list2:
>         ...

Looks interesting.

> but that's another story.
>
> </F>

Thanks for the suggestions,

Jacek





More information about the Python-list mailing list