Functions in Python: Lambda Functions

Capítulo 34

Estimated reading time: 3 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

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:

Continue in our app.
  • Listen to the audio with the screen off.
  • Earn a certificate upon completion.
  • Over 5000 courses for you to explore!
Or continue reading below...
Download App

Download the app

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.

A lambda function in Python is an anonymous function defined with the lambda keyword. It can accept any number of arguments, but must only consist of a single expression. Lambda functions are typically used when a simple, short-lived function is required, such as with map(), filter(), and reduce() functions. They help keep the code concise but should be used judiciously to maintain code readability and debugging simplicity.

Next chapter

Functions in Python: Built-in Functions in Python

Arrow Right Icon
Free Ebook cover System creation course with Python and Django complete
19%

System creation course with Python and Django complete

New course

176 pages

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.