l = list(map(lambda x: x+n, l)) what does this do

Question:

l = [1,2,3,4,5,6,7,8]

print("list : ", l) 
n = int(input('N: ')) 
l = list(map(lambda x: x+n, l)) 
print("new list : ", l)

l = list(map(lambda x: x+n, l)) how does this code works?

Asked By: albert

||

Answers:

lambda use to create function (like def, but it’s inline)

lambda x: x+n mean you insert x as parameter and it will return x+n

For example

f1 = lambda x: x+5
print(f1(10))

result is 15

You can see more here link

map use to apply function to every element in that list or list like such as tuple, array

see more here link

so in your case, if you input n=1, result is l=[2,3,4,5,6,7,8,9]

Answered By: SoulCRYSIS

This code add n to each element of the list. Read more about map and lambda function on https://docs.python.org/3/library/functions.html#map and https://python-reference.readthedocs.io/en/latest/docs/operators/lambda.html

Answered By: Harshit Rathore
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.