← Back to Chapters

Python Lambda Functions

? Python Lambda Functions

⚡ Quick Overview

A lambda function in Python is a small anonymous function defined using the lambda keyword. It can have any number of arguments but only one expression, and that expression is automatically returned.

  • Used for short, throwaway functions.
  • Common with map(), filter(), and sorted().
  • Perfect when you need a function as an argument in just one place.

? Key Concepts

  • Anonymous: Lambda functions usually have no name (unlike functions defined with def).
  • Single expression: They contain exactly one expression, whose result is returned.
  • Any number of arguments: You can pass multiple inputs, but still only one expression.
  • Inline usage: Designed to be used where they are defined, often as arguments.
  • Readability trade-off: Great for short logic, but longer lambdas hurt readability.

? Syntax and Theory

The basic syntax of a lambda function is:

? View Lambda Syntax
lambda arguments: expression
# Basic lambda function syntax

This creates an anonymous function object. For example, lambda x: x * 2 represents a function that doubles its argument.

You can assign a lambda to a variable (giving it a name), or pass it directly into higher-order functions like map(), filter(), and sorted().

? Code Examples

Lambda functions can be used wherever functions are required.

➕ Lambda to add two numbers
# Example 1: Lambda to add two numbers
add = lambda a, b: a + b
print(add(5, 3))  # 8
? Lambda with map() to square numbers
# Example 2: Lambda with map
numbers = [1, 2, 3, 4]
squared = list(map(lambda x: x**2, numbers))
print(squared)  # [1, 4, 9, 16]
? Lambda with filter() to get even numbers
# Example 3: Lambda with filter
numbers = [1, 2, 3, 4]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)  # [2, 4]

? Output and Explanation

▶️ What this code does

  • add(5, 3) uses a lambda to add two numbers and prints 8.
  • map(lambda x: x**2, numbers) applies the lambda to every element in [1, 2, 3, 4], producing [1, 4, 9, 16].
  • filter(lambda x: x % 2 == 0, numbers) keeps only numbers divisible by 2, giving [2, 4].

In all these cases, the lambda replaces a separately defined function. This keeps the code short and focused when the logic is simple.

? Use Cases

  • Short, throwaway functions without defining a named function.
  • Using with functions like map(), filter(), and sorted().
  • When a small function is needed as an argument in only one place.
  • Custom sorting keys, such as sorting a list of tuples by a specific element.

? Tips & Best Practices

  • Use lambda functions for concise, simple operations.
  • Prefer regular def functions if the logic is complex or reused.
  • Remember that lambda functions can only have one expression (no multiple statements).
  • Keep lambdas short so your code remains readable for others (and your future self!).

? Try It Yourself

  • Create a lambda function to multiply two numbers.
  • Use lambda with map() to cube a list of numbers.
  • Use lambda with filter() to extract odd numbers from a list.
  • Use lambda in sorted() to sort a list of tuples by the second element.
  • Rewrite a small existing def function as a lambda (if the logic is simple enough).