Python Tutorial 0/52 lessons ~6 min read Lesson 12

    Lambdas & Functional Tools

    Lambdas are anonymous one-expression functions.

    Course progress0%
    Focus
    8 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Lambdas are anonymous one-expression functions. Combined with map, filter, and sorted(key=...), they enable concise functional pipelines.

    Understanding the topic

    Core concepts to understand:

    • lambda x: x * 2 — single expression only.
    • Common with sorted, filter, max.
    • Prefer comprehensions over map/filter.

    Syntax reference

    Visual flow / code:

    python
    square = lambda x: x * x
    print(square(5)) # 25
    # Sort by key
    people = [{"name": "B", "age": 30}, {"name": "A", "age": 25}]
    people.sort(key=lambda p: p["age"])
    # Filter + map
    nums = [1, 2, 3, 4]
    evens = list(filter(lambda x: x % 2 == 0, nums))
    doubled = list(map(lambda x: x * 2, nums))
    # Pythonic alternative
    evens = [x for x in nums if x % 2 == 0]

    Execution workflow

    1Lambdas & Functional Tools Workflow
    1 / 3

    lambda x

    x * 2 — single expression only.

    Real-world use

    Lambdas shine inside sorted, min, max, and Pandas apply calls — keep them tiny.

    Best practices

    • Use lambdas only for one-liners.
    • Prefer named functions for clarity.
    • Comprehensions usually beat map/filter.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionLambda vs def?+

    Answer

    A lambda is an anonymous function limited to a single expression with an implicit return, while def can contain statements, docstrings, annotations, and multiple lines. Both produce ordinary function objects. Use lambda for tiny throwaway callbacks such as a key= function, and use def when the function needs a name, tests, or clarity in tracebacks.

    Follow-up

    Why does PEP 8 discourage assigning a lambda to a name?
    2IntermediateQuestionWhy prefer comprehensions?+

    Answer

    For transforming or filtering, comprehensions are usually more readable than map or filter with a lambda, since the logic is inline and needs no extra function call. They also read left to right and combine mapping and filtering in one place. map with an existing named function can still be perfectly clear, so it is a matter of readability, not a rule.

    Follow-up

    When is map with a named function still a good choice?
    3BeginnerQuestionWhat's the limit of a lambda?+

    Answer

    A lambda body must be a single expression, so it cannot contain statements such as assignments (except :=), return, try, or loops, and it cannot have a docstring or annotations on its body. Also, lambdas show up as <lambda> in tracebacks, which makes debugging harder. If it needs more than a simple expression, use def.

    Summary

    In summary: Lambdas = inline one-expression fns. Comprehensions are more Pythonic.

    Ready to mark this lesson complete?Track your journey across the entire course.