substituting list comprehensions for map()

Chris Rebert clp2 at rebertia.com
Mon Nov 2 08:59:44 CET 2009


On Mon, Nov 2, 2009 at 12:54 AM, Jon P. <jbperez at gmail.com> wrote:
> I'd like to do:
>
> resultlist = operandlist1 + operandlist2
>
> where for example
>
> operandlist1=[1,2,3,4,5]
> operandlist2=[5,4,3,2,1]
>
> and resultlist will become [6,6,6,6,6].  Using map(), I
> can do:
>
> map(lambda op1,op2: op1 + op2, operandlist1, operandlist2)
>
> Is there any reasonable way to do this via a list comprehension ?

results = [x+y for x,y in zip(list1, list2)]

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list