Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

double with lambda

There are many other cases besides map where we need to pass a function as a parameter to some other function. Many cases the function we pass is some almost trivial function with a single operation in it. In those cases creating a named function like the "double" function in the previous examples is an overkill.

In this example we also used the list function to force the full evaluation of the map object to make it easier to show the results. Normally you probably would not use the list function here.

numbers = [1, 2, 3, 4]

double_numbers = list( map( lambda n: n * 2, numbers) )
print(double_numbers)

Output:

[2, 4, 6, 8]