Annonymous functions (Lambda functions)
Sometimes, naming a function is not worth the trouble. For example when you’re sure the function will only be used once. For such cases, Python offers us anonymous functions, also called lambda functions.
A lambda function can be assigned to a variable, creating a concise way of defining a function:
It gets more interesting when you need to use a function as an argument. In such cases, the function is often used only once. As you may know, map
applies a function to all elements of an iterable object. We can use a lambda when calling map:
In fact, this is a pattern that you’ll see often. When you need to apply a relatively simple operation on each element of an iterable object, using map()
in combination with a lambda function is concise and efficient.