✦ Python Intermediate & OOP Intermediate Python

L2 · Concept 02 · Advanced Python Techniques

Small functions,
powerful ideas.

A lambda gives you a short function expression when one small, clear operation is all you need.

Learning goalWrite simple lambda functions, use one as a sorted() key, and choose def when a named function is clearer.
01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

A lambda is a short function with one expression.

The form lambda arguments: expression creates a function object without a def statement. Its one expression is automatically the result. Lambdas are most useful when a function is short and passed directly to another function.

def double(number):
    return number * 2

double_lambda = lambda number: number * 2

print(double(5))
print(double_lambda(5))

Both calls print 10. The assignment to double_lambda only compares syntax: for a function you name and reuse, def is usually clearer.

10 10

A lambda body is one expression, not a sequence of arbitrary statements. You can pass the function itself as a value; parentheses such as function_name() call it and produce its result.

↳

Choose readability: Use def when the logic has several steps, needs a useful name, or will be reused. A lambda should make the surrounding code easier to read, not shorter at any cost.

Practical examples
add = lambda a, b: a + b
print(add(3, 4))

products = [
    {"name": "Keyboard", "price": 45},
    {"name": "Mouse", "price": 20},
    {"name": "Monitor", "price": 180},
]

sorted_products = sorted(products, key=lambda product: product["price"])
for product in sorted_products:
    print(product["name"], product["price"])

The first line demonstrates two lambda arguments. In the second example, key asks the lambda for each product’s price. sorted() returns a new list and leaves products unchanged.

One argument — one result
double = lambda number: number * 2
print(double(5))

The lambda receives number and returns its doubled value. This is a syntax demonstration; a reusable named function is normally written with def.

Two arguments — one expression
add = lambda a, b: a + b
print(add(3, 4))

Arguments go before the colon. The expression after the colon is the returned result.

Pass a lambda to sorted()
sorted_products = sorted(products, key=lambda product: product["price"])

sorted() calls the lambda for every product and compares the prices it receives. The original products list keeps its order.

Use def when the work grows
def discounted_price(product):
    price = product["price"]
    return price * 0.9

This has a named intermediate value and more than one step, so def communicates the intent more clearly than a complicated lambda.

Read sorted() and lambda from the outside in

  1. 1. Start with sorted()sorted(products, ...) receives the original list and returns a new sorted list.
  2. 2. Notice keykey= receives a function, not the result of calling one. It tells sorted() which comparison value to use.
  3. 3. Follow productFor each dictionary in products, product temporarily refers to that one dictionary.
  4. 4. Read the lambda resultlambda product: product["price"] returns one price for each product.
  5. 5. See the final ordersorted() compares those prices and builds a new list from lowest to highest unless reverse=True is used.

Common mistakes

  • Putting several statements in a lambdaA lambda contains one expression. Use def when you need assignments, several steps, or clearer control flow.
  • Calling a function instead of passing itWrite key=lambda product: ..., not a value produced before sorted() can inspect each product.
  • Choosing the wrong keyReturn product["price"] to sort by price. Returning product["name"] sorts alphabetically instead.
  • Expecting sorted() to change the original listsorted() creates a new list. Store its result if you want to use the new order.
  • Hiding complex logic in a lambdaIf the expression is hard to read, give the work a meaningful def name instead.

Python Playground

Loading Python runtime…

Run the example, then sort by <code>"name"</code>, add <code>reverse=True</code>, and compare the new list with the unchanged original list.

lambda_functions.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

The lambda returns a negative version of each number. Which original number has the smallest key value?

numbers = [3, 1, 4, 2]
result = sorted(numbers, key=lambda number: -number)
print(result)
Choose the output

Practice independently

Mini Challenge — Product sorting

Before writing the lambda, decide which one value each product must return for the order you want.

Your goalPrint product names sorted by price from low to high, then high to low. Bonus: print them alphabetically by name.

product_sorting_challenge.py

Optional hints
  • Decide whether each product should contribute its "price" or its "name" to the comparison.
  • Start with key=lambda product: product["price"]. Add reverse=True when you need the high-to-low order.
Ask Sloby for a hint
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.