Python style.

Fredrik Lundh effbot at telia.com
Wed May 10 04:28:42 EDT 2000


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]
        ...

the slightly more obscure way:

    for item1, item2 in map(None, list1, list2):
        ...

note that future Python versions may also support
something like:

    for item1 in list1; item2 in list2:
        ...

but that's another story.

</F>




More information about the Python-list mailing list