Lesson 23 of 60
โฎPrev Nextโฏ

PY-101 ยท Functions

Lambda functions

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

23of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    A lambda is a small, unnamed function written in one line: lambda x: x * 2 takes x and returns x * 2. It is used where a short function is needed once, most often as the key for sorted(), max() or min(). For anything longer than one expression, use def.

    • Write a lambda
    • Sort with key=lambda
    • Know when def is better

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    Lambda functions, running
    1. products = [("pen", 20), ("bag", 450), ("book", 120)]
    2. cheap_first = sorted(products, key=lambda p: p[1])
    3. print(cheap_first)
    4. priciest = max(products, key=lambda p: p[1])
    5. print("Most expensive:", priciest[0])
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemYou have a list of products and want them sorted by price, not by name. sorted() needs a tiny rule saying "look at the price". A lambda is that rule.
    2. The Python idealambda parameters: expressionA lambda is a small, unnamed function written in one line: lambda x: x * 2 takes x and returns x * 2.
    3. In real lifeLeaderboards sort players by score with key=lambda p: p.score, often with reverse=True for highest first.

    Start with a question

    You have a list of products and want them sorted by price, not by name. sorted() needs a tiny rule saying "look at the price". A lambda is that rule.

    Sorting and picking the best item by some rule is constant in real code, and lambdas keep those rules right where they are used.

    Explain it like I'm 10

    A lambda is a sticky note with one instruction on it, handed to someone else to use. No name, no fuss.

    The technical explanation

    lambda x: x * 2 is a function that returns x * 2. It has no return keyword; the expression is returned.

    sorted(items, key=lambda p: p[1]) sorts by the second element of each item.

    A lambda can take several parameters: lambda a, b: a + b.

    Syntax

    lambda parameters: expression
    sorted(items, key=lambda item: ...)

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Leaderboards sort players by score with key=lambda p: p.score, often with reverse=True for highest first.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. What does (lambda x: x + 1)(4) return?
    2. 2. Can a lambda contain several statements?
    3. 3. Most common use of lambda?
    4. 4. sorted([3, 1, 2], key=lambda n: -n)?
    5. 5. Does lambda need the return keyword?

    Questions people ask

    When should I use lambda in Python?

    For a short, one-off function passed to another function, especially key= in sorted, min and max. Use def for anything you name or reuse.

    Is lambda faster than def?

    No. They create the same kind of function object. The difference is only how you write it.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner When should I use lambda in Python?

    For a short, one-off function passed to another function, especially key= in sorted, min and max. Use def for anything you name or reuse.

    beginner Is lambda faster than def?

    No. They create the same kind of function object. The difference is only how you write it.

    intermediate What does (lambda x: x + 1)(4) return?

    5. It adds 1 to 4.

    intermediate Can a lambda contain several statements?

    No, one expression only. A lambda is a single expression.

    Quick revision

    Remember

    • Write a lambda
    • Sort with key=lambda
    • Know when def is better
    • If you are tempted to give a lambda a name (double = lambda x: x * 2), write a normal def instead. It is clearer and shows up properly in error messages.

    Syntax

    lambda parameters: expression
    sorted(items, key=lambda item: ...)

    Official documentation