a = input()
print( map(lambda x,y: x+y ,a.split(" ")))
i want to add two numbers
a = input()
print( map(lambda x,y: x+y ,a.split(" ")))
i want to add two numbers
a = input()
print(sum(list(map(int,a.split(" ")))))
lol it was easy
You want ‘to add two numbers’, right? As input() returns a string, after split() it will return a list of characters. so firstly we have to convert them to int that can be done using map
map(int, a.split())
Now lets see what map function do under the hood with the list:
map(fun, seq): The first argument func is the name of a function and the second a sequence (e.g. a list) seq. map() applies the function func to all the elements of the sequence seq. It returns a new list with the elements changed by func.
So as we can see map returns a list, do we need a list ? and even it will never take two lamda argument (lambda x,y) from a single list, for two arguments we need two lists with the same size (for further explanation on map use this link)
So over all for this set of work, we need reduce function (which is no more available in python 3, you can use it through functools.reduce(function, iterable[, initializer]))
reduce returns a single value after applying the function to each of the elements, the function can take two arguments i.e, two elements from the same array.
reduce(lambda x,y: x+y, [47,11,42,13])
= 113
The following diagram shows the intermediate steps of the calculation:
so the final code for this would be:
reduce(lambda x,y: x+y, map(int, a.split())) #python ver2
functools.reduce(lambda x,y: x+y, map(int, a.split())) #python ver3
for more information related to reduce link