Python style.

emile at fenx.com emile at fenx.com
Wed May 10 22:57:48 CEST 2000


Which brings to mind this slightly altered form:

>>> list1 = [1,2,3,4,5,6]
>>> list2 = [6,5,4,3,2,1]
>>> for x in map(lambda list1, list2: list1 - list2, list1,list2): print x

... and since we're on the subject of style, and I don't recall having
seen the 
iterators of a map(lambda... use the names of the objects they iterate
over,
does anyone have any pros or cons to offer on this style?

Emile van Sebille
emile at fenx.com

Chuck Esterbrook <echuck at mindspring.com> wrote in message
news:<3919A7AB.175BFACF at mindspring.com>...
> Jacek Generowicz 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.
> > 
> > Thanks,
> > 
> > Jacek
> 
> 
> I'm surprised I haven't seen this version, yet:
> 
> 
> for x in map(lambda a,b: a-b, list1, list2):
>     print x 
> 
> 
> That doesn't feel obscure to me and in most of my programs I'm passing my results around more often than I'm directly dumping them to the console (where they're no longer usable). The point is I would probably "return" the map() above or stick in a var, except for really simple programs.
> 
> -Chuck
> -- 
> http://www.python.org/mailman/listinfo/python-list
>




More information about the Python-list mailing list