🐍

Lambda Functions

Programare Python Intermediate 5 min read

Lambda Functions

Syntax

lambda parameters: expression

Examples

square = lambda x: x ** 2
add = lambda a, b: a + b

# With sorted
sorted(students, key=lambda s: s[1])

# With map/filter
list(map(lambda x: x * 2, numbers))
list(filter(lambda x: x > 0, numbers))

Limitations

  • Single expression only
  • No statements
  • No docstrings

Key Points

  • Anonymous functions
  • Auto-returns expression result
  • Best for short, simple operations

📚 Related Articles