Search code examples
pythonlistlambdagenerator

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


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?


Solution

  • 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]