30. Lambda Functions in Python

Quiz: 0/5

Lambda functions in Python are concise, anonymous functions that allow you to perform small, one-line operations without using the def keyword. They are ideal for situations where you need a quick, temporary function.

In this lesson, we will explore the syntax, examples, and best use cases for lambda functions.

Syntax of Lambda functions

The general syntax for a lambda function is as follows:

lambda arguments: expression

Explanation:

  • lambda Keyword: This marks the beginning of a lambda function.
  • arguments: A comma-separated list of parameters (just like in regular functions). Lambda functions can accept multiple arguments.
  • expression: A single expression evaluated and returned by the lambda function. Unlike regular functions, there is no return keyword—the result is automatically returned.

Examples of Lambda functions

Let’s dive into some examples to understand how lambda functions work.

a. Simple Lambda function

# A lambda function that adds 10 to the input number
add_ten = lambda x: x + 10

result = add_ten(5)  # Calling the lambda function
print(result)  # Output: 15

Explanation:

  • The lambda function add_ten takes one argument x and returns x + 10.
  • When called with 5, it returns 15.

b. Lambda function with multiple arguments

# A lambda function that multiplies two numbers
multiply = lambda x, y: x * y

result = multiply(4, 5)  # Calling the lambda function
print(result)  # Output: 20

Explanation:

  • The lambda function multiply takes two arguments x and y and returns their product.
  • When called with 4 and 5, it returns 20.

c. Using Lambda functions in higher-order functions

Lambda functions are frequently used in combination with higher-order functions like map(), filter(), and reduce().

Example with map():

# Using lambda with map to square each number in a list
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x ** 2, numbers))

print(squared)  # Output: [1, 4, 9, 16, 25]

Explanation:

  • The lambda function takes one argument x and returns x ** 2 (x squared).
  • The map() function applies this lambda function to each element in the numbers list, producing a new list of squared values.

When to use Lambda functions

Lambda functions are particularly useful in the following scenarios:

  1. Short, Temporary Functions: When you need a small, unnamed function for a brief task, such as passing it as an argument to another function.

  2. Functional programming: Lambda functions are often used in functional programming contexts with functions like map(), filter(), and reduce().

  3. Inline operations: When defining a full function using def would be excessive for simple, one-liner operations.

Limitations of Lambda functions

While lambda functions are powerful, they come with some restrictions:

  • Single Expression Only: A lambda function can only contain a single expression and cannot include multiple lines or statements.

  • No Name: Lambda functions are anonymous, which makes them harder to debug or reuse compared to named functions.

  • Reduced Readability: Overusing lambda functions can make code less readable, especially when they are complex.

 

Hands-On Practise

Exercise: Using a Lambda function with filter()

Write a Python program that uses a lambda function with the filter() function to extract only even numbers from the given list:

numbers = [10, 15, 20, 25, 30, 35, 40]

Instructions:

  1. Use a lambda function inside filter() to check if a number is even.
  2. Convert the result of filter() into a list.
  3. Print the list of even numbers.
Output:

Quizzes: 0/5

Question 1:

What is the purpose of a lambda function in Python?

  • To define a class in a single line.
  • To create small anonymous functions for short operations.
  • To handle errors in Python programs.
  • To optimize loops in Python.

Question 2:

What is the correct syntax for a lambda function?

  • lambda x: x + 10
  • def lambda(x): x + 10
  • lambda x, x + 10
  • lambda(x): x + 10

Question 3:

Which of the following is NOT a valid use case for lambda functions?

  • Using with map() to apply a function to each element in a list.
  • Using with filter() to filter elements based on a condition.
  • Creating a large, complex function with multiple statements.
  • Using with reduce() to combine elements in a list.

Question 4:

What will be the output of the following code? add = lambda x, y: x + y print(add(5, 10))

  • 15
  • 510
  • SyntaxError
  • None

Question 5:

What does the following code do? numbers = [1, 2, 3, 4, 5] squared = list(map(lambda x: x ** 2, numbers))

  • Creates a list of numbers multiplied by 2.
  • Creates a list of the squares of the numbers.
  • Creates a list of numbers divided by 2.
  • Generates an error because lambda cannot be used with map().

Feedback

Share your thoughts about this lesson

Discussion

Please log in to create a discussion.
Previous lessonUsing the return statement in Python Functions