Python style.

Neil Schemenauer nascheme at enme.ucalgary.ca
Wed May 10 04:27:03 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
>
>In other words, I want to loop through two lists,
>performing some operation on elements in
>corresponding positions . . . there must be a more
>elegant way.

You code looks okay.  You could do:

    for i in range(len(list1)):
        print list1[i] - list2[i]

But that's not too much different.

    Neil

-- 
"With a PC, I always felt limited by the software available. On
Unix, I am limited only by my knowledge."
        --Peter J. Schoenster <pschon at baste.magibox.net>



More information about the Python-list mailing list