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
10A 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.
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.
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.
add = lambda a, b: a + b
print(add(3, 4))Arguments go before the colon. The expression after the colon is the returned result.
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.
def discounted_price(product):
price = product["price"]
return price * 0.9This 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. Start with sorted()
sorted(products, ...)receives the original list and returns a new sorted list. - 2. Notice key
key=receives a function, not the result of calling one. It tellssorted()which comparison value to use. - 3. Follow productFor each dictionary in
products,producttemporarily refers to that one dictionary. - 4. Read the lambda result
lambda product: product["price"]returns one price for each product. - 5. See the final order
sorted()compares those prices and builds a new list from lowest to highest unlessreverse=Trueis used.
Common mistakes
- Putting several statements in a lambdaA lambda contains one expression. Use
defwhen you need assignments, several steps, or clearer control flow. - Calling a function instead of passing itWrite
key=lambda product: ..., not a value produced beforesorted()can inspect each product. - Choosing the wrong keyReturn
product["price"]to sort by price. Returningproduct["name"]sorts alphabetically instead. - Expecting sorted() to change the original list
sorted()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
defname instead.
