jmg at ecs.soton.ac.uk
Wed May 10 10:49:18 CEST 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
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:
> but that's another story.
Thanks for the suggestions,
More information about the Python-list