0

when I use lambda as a function,the result is list connection. but when I use lambda with map,the result is the sum of two lists.

res = [[1]]
g = lambda x, y: x + y
print(g(res[-1] + [0], [0] + res[-1]))
print(list(map(lambda x, y: x + y, res[-1] + [0], [0] + res[-1])))

[1, 0, 0, 1] [1, 1]

2
  • 1
    You answered your own question. Commented May 12, 2019 at 14:55
  • Is this a trick for using lambda with map? Commented May 12, 2019 at 14:59

1 Answer 1

1

This line is straightforward:

print(g(res[-1] + [0], [0] + res[-1]))

It calls the function just once with arguments [1, 0] and [0, 1]. These two list concatenated produce [1,0,0,1]


UPDATED AND CORRECTED:

My first answer was describing the case with one iterable, but this is a map with two iterables.

The line with map calls the function (lambda or regular, does not matter) one time for each set of arguments. How exactly are these arguments formed is described in the docs:

Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted.

I have reformatted that line:

print(list(map(
    lambda x, y: x + y,  # call this function repeatedly
    res[-1] + [0],       # X's are takes from this list,
    [0] + res[-1]        # Y's are taken from this list,
    ))) 

There will be as many calls as there are values in the lists (in the shortest one, to be exact). First call has arguments 1, 0; second call's arguments are 0, 1. Each set of arguments has two values, because there are two lists passed (lists are iterables, of course) so it matches the function which expects two arguments (x and y). If there were three or more iterables, a TypeError would occur.

First call adds 1+0, second call adds 0+1. The resulting list is [1,1].

AnOther example (100+1, 200+2, 300+3) -> [101,202,303]:

print(list(map(
    lambda x, y: x + y,  # call this function repeatedly
    [100,200,300],       # X's are takes from this list,
    [1,2,3]              # Y's are taken from this list,
    )))

You may add debugging output to the definition of g to see what is being computed.

def g(x, y): 
    print(f"debug: {x} + {y} -> {x+y}")
    return x+y 

It makes no difference if the function is defined as lambda or not. You can write the map like this:

print(list(map(g, res[-1] + [0], [0] + res[-1])))

and test the program with different data.

Sign up to request clarification or add additional context in comments.

2 Comments

But when res = [[1], [1,1]] print ([list(map(lambda x, y: x+y, res[-1] + [0], [0] + res[-1]))]) >>[1,2,1] there,res[-1]+[0] ->[1,1,0],[0] + res[-1]->[0,1,1];each of these two lists include 3 values.Could you please explain why?
@DavidZhang I posted an updated answer. I'm sorry for the mistake in the first answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.