Lesson 14 of 25

Lambda Functions

Lambda Expressions

A lambda function is a small, anonymous function defined in a single line using the 'lambda' keyword. Lambdas are useful for short, throwaway functions that you don't need to reuse elsewhere.

The syntax is: lambda parameters: expression. The expression is automatically returned — no 'return' keyword needed.

Example
# Regular function
def square(x):
    return x ** 2

# Equivalent lambda
square = lambda x: x ** 2
print(square(5))  # 25

# Lambda with multiple parameters
add = lambda a, b: a + b
print(add(3, 5))  # 8

# Common use: sorting with a key function
students = [
    {"name": "Charlie", "grade": 85},
    {"name": "Alice", "grade": 92},
    {"name": "Bob", "grade": 78}
]

# Sort by grade
students.sort(key=lambda s: s["grade"])
for s in students:
    print(f"{s['name']}: {s['grade']}")
# Bob: 78
# Charlie: 85
# Alice: 92
  • lambda arguments: expression — defines an anonymous function
  • Lambdas can only contain a single expression (no statements)
  • The expression result is automatically returned
  • Most commonly used as arguments to higher-order functions like sort(), map(), filter()
Try Lambda Functions
JavaScript
# Lambda basics
square = lambda x: x ** 2
add = lambda a, b: a + b

print("Square of 5:", square(5))
print("3 + 7:", add(3, 7))

# Sorting with lambda
words = ["banana", "pie", "Washington", "book"]
words.sort(key=lambda w: len(w))
print("Sorted by length:", words)
Notes
  • PEP 8 discourages assigning lambda functions to variables. If you need a named function, use a regular def statement. Use lambdas inline where a function object is required.

map(), filter(), and reduce()

Python provides built-in higher-order functions that work beautifully with lambdas. map() applies a function to every item in an iterable, filter() selects items matching a condition, and reduce() accumulates a single result.

While list comprehensions can often replace map() and filter(), these functions are still useful when working with existing functions or for functional programming patterns.

Example
# map() — apply a function to each item
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x ** 2, numbers))
print(squared)  # [1, 4, 9, 16, 25]

# filter() — keep items where function returns True
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)  # [2, 4, 6, 8, 10]

# reduce() — accumulate a single value (must import)
from functools import reduce
numbers = [1, 2, 3, 4, 5]
total = reduce(lambda a, b: a + b, numbers)
print(total)  # 15  (1+2=3, 3+3=6, 6+4=10, 10+5=15)

# Combining map and filter
numbers = range(1, 11)
result = list(map(lambda x: x ** 2, filter(lambda x: x % 2 == 0, numbers)))
print(result)  # [4, 16, 36, 64, 100]

# Equivalent list comprehension (often preferred)
result = [x ** 2 for x in range(1, 11) if x % 2 == 0]
print(result)  # [4, 16, 36, 64, 100]
Try map() and filter()
JavaScript
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

squared = list(map(lambda x: x ** 2, numbers))
print("Squared:", squared)

evens = list(filter(lambda x: x % 2 == 0, numbers))
print("Evens:", evens)

from functools import reduce
total = reduce(lambda a, b: a + b, numbers)
print("Sum:", total)
Notes
  • In modern Python, list comprehensions are generally preferred over map() and filter() for readability. However, map() can be useful when you already have a named function to apply.