6.8 Functions in Python: Lambda Functions

Python functions are reusable blocks of code that perform specific tasks within a program. They are an essential part of Python programming and are used to improve code readability, avoid code repetition, and allow code reuse. One such function is the lambda function, also known as an anonymous function.

What is a lambda function?

A lambda function is a small anonymous function that is defined with the lambda keyword. Lambda functions can take any number of arguments, but they can only take one expression. Its syntax is:

lambda arguments: expression

Arguments are the values ​​you pass to the function. The expression is what you want the function to do with those arguments.

Using lambda functions

Lambda functions are used when you need a small, anonymous function for a short period of time. They are most commonly used with the built-in map(), filter() and reduce() functions.

For example, let's use a lambda function with the map() function to fold all the numbers in a list:

numbers = [1, 2, 3, 4, 5]
doubled = map(lambda x: x * 2, numbers)
print(list(doubled)) # Prints: [2, 4, 6, 8, 10]

In this example, the lambda function takes an argument x and returns x * 2. The map() function applies the lambda function to all elements of the numbers list and returns a map object. We convert this object to a list and print the list.

Why use lambda functions?

Lambda functions are useful when you need a small, anonymous function for a short period of time. They're especially useful when you're working with functions like map(), filter(), and reduce() that expect a function as one of their arguments.

Lambda functions can also make your code more concise and readable by eliminating the need to define and name extra functions. However, they should be used sparingly, as they can make code harder to understand if used in excess.

Limitations of lambda functions

While lambda functions are powerful, they have their limitations. The biggest limitation is that they can only have one expression and cannot include statements. This means that you cannot use loops, conditionals, or assignment operators in a lambda function.

Also, lambda functions don't have a name, which can make the code harder to debug and understand. If you need a more complex function or want to reuse the function in multiple places, it might be better to define a regular function using the def keyword.

In conclusion, lambda functions are a useful tool to have in your Python programming arsenal. They allow you to create small, anonymous functions at runtime, making your code more concise and readable. However, they should be used sparingly and carefully, due to their limitations and the potential to make code more difficult to understand.

Now answer the exercise about the content:

What is a lambda function in Python and how is it used?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Functions in Python: Built-in Functions in Python

Next page of the Free Ebook:

35Functions in Python: Built-in Functions in Python

3 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text