Unit 3 • Lesson 9

Lambda Functions

Overview

Lambda functions are small, anonymous functions that you can create on the fly. You'll learn how to write simple functions in a single line, use them with functions like map() and filter(), and understand when lambda functions make your code more concise and readable.

Intermediate 20–25 min

What You Will Learn in This Lesson

By the end of this lesson, you will know:

  • What lambda functions are: Understand anonymous functions in Python.
  • Lambda syntax: Learn how to write lambda functions.
  • Using lambdas: Discover when and how to use lambda functions.
  • Lambda vs regular functions: Understand when to use each.

Why This Matters

Lambda functions let you write small, simple functions quickly without needing to define them with def. They're especially useful when you need a function for a short, one-time use - like passing a function to another function. While they're not always necessary, they can make your code more concise and readable when used appropriately.

Step 1: What Is a Lambda Function?

A lambda function is a small, anonymous function that you can create in a single line. It's called "lambda" after the Greek letter λ, which is used in mathematics to represent functions. Lambda functions are useful for simple operations that don't need a full function definition.

Lambda Function Syntax
# Regular function
def add(x, y):
    return x + y

# Lambda function (same thing)
add = lambda x, y: x + y

print(add(3, 5))  # Output: 8
1

Use lambda Keyword

Start with the lambda keyword instead of def. This tells Python you're creating an anonymous function.

2

List Parameters

After lambda, list the parameters (like x, y). No parentheses needed around the parameters.

3

Add Expression

After a colon (:), write the expression that the function returns. Lambda functions automatically return the result of this expression - no return keyword needed.

Key Concept: Lambda functions are limited - they can only contain a single expression, not multiple statements. If you need multiple lines of code or complex logic, use a regular function with def instead. Lambda functions are best for simple, one-line operations.

Mini Practice #1: Your First Lambda

Try It Yourself

Try creating and using a lambda function:

Press Run to see output

What happened? The lambda function lambda x: x * x takes one parameter x and returns x * x. You can assign it to a variable (square) and use it just like a regular function. When you call square(5), it calculates 5 * 5 = 25. Lambda functions are a quick way to create simple functions without writing a full def statement!

Step 2: Lambda with Multiple Parameters

Lambda functions can take multiple parameters, just like regular functions:

Lambda with Multiple Parameters
multiply = lambda x, y: x * y
add = lambda a, b, c: a + b + c

print(multiply(3, 4))      # Output: 12
print(add(1, 2, 3))        # Output: 6

How It Works

You can list multiple parameters separated by commas. The lambda function will take all those parameters and use them in the expression. Just like regular functions, the number of arguments you provide must match the number of parameters.

Step 3: Using Lambda with Built-in Functions

Lambda functions are commonly used with functions like map() and filter():

Lambda with map()
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x * x, numbers))
print(squared)  # Output: [1, 4, 9, 16, 25]
1

map() Function

The map() function applies a function to every item in a list. It takes two arguments: the function to apply, and the list to process.

2

Lambda as Function

Instead of defining a separate function, you can pass a lambda function directly to map(). This is more concise than defining a function just to use it once.

3

Convert to List

map() returns a map object, so you need to convert it to a list using list() to see the results.

Remember: Lambda functions are perfect for simple operations that you only need once. If you find yourself using the same lambda multiple times, or if the logic is complex, consider using a regular function instead.

Mini Practice #2: Lambda with filter()

Try It Yourself

Try using a lambda function with filter() to find even numbers:

Press Run to see output

What happened? The filter() function keeps only items from a list that meet a condition. The lambda function lambda x: x % 2 == 0 checks if a number is even (divisible by 2). filter() applies this lambda to each number and keeps only the even ones. This is much more concise than writing a separate function just to check if a number is even!

Step 4: When to Use Lambda Functions

Lambda functions are best for:

Simple Operations

One-line expressions that are easy to understand.

double = lambda x: x * 2

One-Time Use

Functions you only need once, like with map() or filter().

list(map(lambda x: x*2, nums))

When NOT to Use Lambda

Don't use lambda functions for complex logic, multiple statements, or functions you'll use multiple times. Regular functions with def are better for those cases because they're easier to read, test, and debug.

End-of-Lesson Exercises

Exercise 1: Create a Lambda Function

Create a lambda function that takes one parameter number and returns number * 3. Assign it to a variable named triple and use it to triple the number 7.

Use lambda keyword with the syntax: lambda parameter: expression

Write your code above and click "Check Answer" to verify it's correct.

Exercise 2: Use Lambda with map()

Create a list of numbers [1, 2, 3, 4, 5]. Use map() with a lambda function to square each number. Convert the result to a list and print it.

Use map(lambda x: x * x, numbers) and convert to list.

Write your code above and click "Check Answer" to verify it's correct.