Python Lambda Functions

Let’s look at lambda functions

  1. Notice

    lambda creates a function from a single expression. The expression’s value becomes the return value.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
double = lambda number: number * 2
print(double(3))
Live state

Top-level scope

double→Object 1
Object 1 · functionfunction <lambda>
This step

lambda creates a function from a single expression. The expression’s value becomes the return value.

Picture lambda functions

lambda creates a function from a single expression. The expression’s value becomes the return value.

Image brief

Alt text
A diagram for Python Lambda Functions. Instructions: double = lambda number: number * 2 print(double(3)). Displayed result: 6. The final example bindings are double --> object 1 (function). object 1: function <lambda>
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Lambda Functions". Teaching focus: lambda creates a function from a single expression. The expression’s value becomes the return value. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS double = lambda number: number * 2 print(double(3)) | v DISPLAYED RESULT 6 FINAL BINDINGS double --> object 1 (function) object 1: function <lambda> Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python Lambda Functions. Instructions: double = lambda number: number * 2 print(double(3)). Displayed result: 6. The final example bindings are double --> object 1 (function). object 1: function <lambda>

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
double = lambda number: number * 2
print(double(3))
Live state

Top-level scope

double→Object 1
Object 1 · functionfunction <lambda>
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
6

What do you expect this program to print?

What does this example display?

double = lambda number: number * 2
print(double(3))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

6

Follow the printed values in order. The output is: 6

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

lambda creates a function from a single expression. The expression’s value becomes the return value.

lambda creates a function from a single expression. The expression’s value becomes the return value.

What is a Lambda Function?

A lambda function is a small, anonymous function defined using the lambda keyword instead of def. Unlike regular functions, lambda functions don’t have a name and are generally used for short, throwaway operations.

Basic Syntax

The basic syntax of a lambda function in Python is:

lambda arguments: expression

A lambda function can take any number of arguments, but it must contain only a single expression. It returns the result of that expression automatically — no need to use the return keyword.

Example: Add Two Numbers

add = lambda x, y: x + y
print(add(5, 3))
8

Explanation:

  • lambda x, y: defines an anonymous function that takes two parameters: x and y.
  • The expression x + y is evaluated and returned as the result of the function.
  • This lambda function is assigned to the variable add, so you can call it like a regular function using add(5, 3).
  • When called with arguments 5 and 3, it returns 8, which is printed to the output.

Sorting with Lambda

Lambda functions are particularly useful when passed as arguments to higher-order functions. Consider the example below where we sort a list of tuples by the second element:

data = [(1, 4), (2, 1), (3, 9)]
sorted_data = sorted(data, key=lambda x: x[1])
print(sorted_data)
[(2, 1), (1, 4), (3, 9)]

Filtering with Lambda

Use lambda with filter() to select only even numbers from a list:

numbers = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)
[2, 4, 6]

Mapping with Lambda

Apply a function to every element using map():

nums = [1, 2, 3, 4]
squares = list(map(lambda x: x**2, nums))
print(squares)
[1, 4, 9, 16]

When Should You Use Lambda?

  • When the function is short and simple.
  • When the function will not be reused elsewhere.
  • When you need a function as an argument (e.g., in map, filter, sorted).

When NOT to Use Lambda

Lambda functions come with limitations. Avoid them when:

  • The logic inside is too complex.
  • You want to reuse or debug the function later.
  • You need statements (lambda only allows expressions).

Common Mistakes and Checks

  • Only one expression is allowed. Statements like if/else, try/except, and print() inside lambda are not allowed unless used as expressions.
  • Readability matters. Just because you can use a lambda doesn’t mean you should. Avoid sacrificing clarity.
  • Debugging is hard. Anonymous nature makes it difficult to trace errors.

Example: Conditional Expression in Lambda

max_func = lambda a, b: a if a > b else b
print(max_func(10, 20))
20

Lambda vs Regular Function

def add_def(x, y):
    return x + y

add_lambda = lambda x, y: x + y

print(add_def(2, 3))    # Output: 5
print(add_lambda(2, 3)) # Output: 5
5
5

How to Test and Verify

  • Always start with small examples. Try printing the output after defining a lambda.
  • Use type() to confirm it's a function: print(type(lambda x: x))
  • Compare behavior side-by-side with a regular function to understand what’s happening.

What to remember

Connect what you noticed in the example with the idea below.

Remember

lambda creates a function from a single expression. The expression’s value becomes the return value.

Watch out for this idea

A lambda body can contain a sequence of ordinary statements.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python File Operations →