Use multiple lists as input arguments of a function (Python)

Sarah picture Sarah · Dec 2, 2015 · Viewed 7.6k times · Source

I know that the property map(function,list) applies a function to each element of a single list. But how would it be if my function requires more than one list as input arguments?.

For example I tried:

   def testing(a,b,c):
      result1=a+b
      result2=a+c
      return (result1,result2)

a=[1,2,3,4]
b=[1,1,1,1]
c=[2,2,2,2]

result1,result2=testing(a,b,c)

But this only concatenates the arrays:

result1=[1,2,3,4,1,1,1,1]
result2=[1, 2, 3, 4, 2, 2, 2, 2]

and what I need is the following result:

result1=[2,3,4,5] 
result2=[3,4,5,6]

I would be grateful if somebody could let me know how would this be possible or point me to a link where my question could be answered in a similar case.

Answer

CubeRZ picture CubeRZ · Dec 2, 2015

You can use operator.add

from operator import add

def testing(a,b,c):
    result1 = map(add, a, b)
    result2 = map(add, b, c)
    return (result1, result2)