Lambda Functions in Python

    Kedar Kabra3 min readUpdated

    A lambda is a small, one-line function with no name. It's handy when you need a quick function to pass to another function. Compare these two — they do exactly the same thing:

    # A normal function
    def double(x):
        return x * 2
    
    # The same thing as a lambda
    double = lambda x: x * 2
    
    print(double(5))     # 10 either way
    

    The lambda syntax is lambda arguments: expression. There's no return — the expression's value is returned automatically.

    When are lambdas useful?

    You rarely assign a lambda to a variable like above (a normal def is clearer for that). Lambdas shine when you pass a tiny function into another function. The most common case is the key argument of sorted():

    students = [
        ("Asha", 85),
        ("Ravi", 72),
        ("Meena", 91),
    ]
    
    # Sort by the second item (the score), highest first
    ranked = sorted(students, key=lambda student: student[1], reverse=True)
    print(ranked)
    # [('Meena', 91), ('Asha', 85), ('Ravi', 72)]
    

    Here the lambda tells sorted() what to sort by. Writing a separate named function for such a small job would be overkill.

    Lambdas with map() and filter()

    map() applies a function to every item; filter() keeps only items where the function returns True:

    numbers = [1, 2, 3, 4, 5, 6]
    
    # map: square every number
    squares = list(map(lambda n: n ** 2, numbers))
    print(squares)        # [1, 4, 9, 16, 25, 36]
    
    # filter: keep only even numbers
    evens = list(filter(lambda n: n % 2 == 0, numbers))
    print(evens)          # [2, 4, 6]
    

    Note that map() and filter() return special iterator objects, so we wrap them in list() to see the result.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Lambda with multiple arguments

    A lambda can take more than one argument, separated by commas:

    # A lambda that adds two numbers
    add = lambda a, b: a + b
    print(add(3, 4))      # 7
    
    # Useful inside functions like sorted with a tuple key
    words = ["banana", "apple", "fig", "cherry"]
    # Sort by length, then alphabetically
    words.sort(key=lambda w: (len(w), w))
    print(words)          # ['fig', 'apple', 'banana', 'cherry']
    

    A practical example

    # Find the product with the lowest price
    
    products = [
        {"name": "Pen", "price": 20},
        {"name": "Notebook", "price": 60},
        {"name": "Eraser", "price": 10},
    ]
    
    # min() uses the lambda to compare by price
    cheapest = min(products, key=lambda item: item["price"])
    print(f"Cheapest: {cheapest['name']} at {cheapest['price']}")
    # Cheapest: Eraser at 10
    

    Common mistakes

    • Trying to put statements in a lambda: A lambda can only hold a single expression — no if/else blocks, no loops, no assignments. If you need those, use a regular def.
    • Overusing lambdas: Assigning a complex lambda to a variable hurts readability. If it's more than a trivial line, a named function is clearer and shows up better in error messages.
    • Forgetting that map/filter need list(): print(map(...)) shows a map object, not the values. Wrap it in list().
    • Confusing the syntax: It's lambda x: x + 1, with a colon and no parentheses around the parameter. No return keyword inside.
    • Late binding in loops: Creating lambdas inside a loop that reference the loop variable can capture the wrong value. This is an advanced gotcha — use a default argument (lambda x, n=n: ...) if you hit it.

    FAQ

    What does "anonymous function" mean? A function without a name. A lambda is anonymous because you usually use it on the spot without giving it a permanent name.

    Is a lambda faster than a normal function? No. Performance is the same. Lambdas are about convenience and conciseness, not speed.

    Can I use an if-else in a lambda? Only the expression form: lambda x: "even" if x % 2 == 0 else "odd". You cannot use block statements.


    Lambdas build on what you learned in Functions in Python. They also pair beautifully with List Comprehensions in Python for transforming data. Explore more on the Python learning hub.

    Want to learn this properly? Join the waitlist for our Python course — taught in Jalgaon, beginner-friendly.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Kedar Kabra

    Instructor, Infoplanet

    Kedar Kabra teaches Python at Infoplanet, helping beginners become confident programmers through hands-on, project-first practice.

    Related guides