ComparisonComparisons

lambda vs def

lambda creates a short, anonymous function with one expression, while def defines a named function that can have multiple statements, default values, and a docstring.

On this page

Side-by-side comparison

#
  • Number of statements in the body

    lambda
    A single expression
    def
    Any number of statements
  • The return keyword

    lambda
    Not allowed, the result is automatic
    def
    Required to return a value
  • Function name

    lambda
    None, it shows up as <lambda>
    def
    Its own name, visible in error tracebacks
  • Docstring

    def
    Yes
  • Assigning to a variable

    lambda
    Possible, but not recommended for complex logic
    def
    The standard way of defining a function
  • Typical use

    lambda
    A short argument, e.g. key= in sorted()
    def
    Reusable logic, longer functions

Example

#
Python
students = [("Emma", 92), ("Jack", 78)]

by_score_lambda = sorted(students, key=lambda s: s[1], reverse=True)

def by_score(student):
    return student[1]

by_score_def = sorted(students, key=by_score, reverse=True)

print(by_score_lambda)
print(by_score_lambda == by_score_def)
Resultsaved result, you can check it
[('Emma', 92), ('Jack', 78)]
True

Definition and usage

#

Both create a function you can call, pass along as an argument, or assign to a variable. In both cases the parameters work the same way: positional, keyword, with default values.

They differ in what they can contain. After lambda there’s only a single expression, whose result is returned automatically, with no return keyword and no room for conditional statements or loops. def opens an indented block that can hold any number of statements, with its own returns, ifs, and loops, as well as a docstring. A function defined with def has its own name, which shows up in error tracebacks, while a lambda appears there as <lambda>.

Use lambda when the function is short and needed in just one place, most often as an argument, e.g. key= in sorted() or a function in map(). Use def when the logic is longer, spans more than one line, should have a name and a docstring, or when you use it in several places.

Syntax

#
Syntax
lambda parameters: expression

def name(parameters):
    return value

More examples

#
lambda can only contain a single expression
Python
describe = lambda score: "passed" if score >= 60 else "failed"
print(describe(75))
print(describe(40))
Resultsaved result, you can check it
passed
failed
def has a name visible in the code, lambda doesn’t
Python
def divide(a, b):
    return a / b

divide_lambda = lambda a, b: a / b

print(divide.__name__)
print(divide_lambda.__name__)
Resultsaved result, you can check it
divide
<lambda>

Good practices

#
  • If a lambda starts getting long or nested, turn it into a regular function with def. It’ll be easier to read and to test.
  • When sorting by a field, also consider operator.itemgetter(1) or operator.attrgetter("points") instead of a lambda.
  • Don’t assign a lambda to a variable just to give it a name (f = lambda x: x + 1). It’s better to write def f(x): return x + 1 directly.

Related entries

#

Spotted a mistake or missing an example? Write to us.