PYTHON Reference Guide
Revision Time: 2 mins

Lambda Reference

Anonymous single-line expression functions.

Basic Lambda Expressions
Return: Callable

Define anonymous small functions.

Used In: Key sorting metrics (e.g. sorted(d, key=lambda x: x[1])), quick filtering.

Syntax signature:lambda args: expression
Code snippet:
python
square = lambda x: x * x
print(square(4))
Expected Output:16

Time Complexity: O(1)

Common Mistakes: Using lambda for complex functions reduces code readability.

Related Methods: map(), filter()

Comparison:

def function vs lambda: def defines a reusable, named function with multiple statements; lambda defines an anonymous, inline single-expression function.

Remember: Limited to a single expression.
map() with Lambda
Return: map object

Apply a lambda transformation function to all elements in an iterable.

Used In: Converting currencies, converting list elements to uppercase.

Syntax signature:map(lambda item: expr, iterable)
Code snippet:
python
lst = [1, 2, 3]
res = map(lambda x: x * 10, lst)
print(list(res))
Expected Output:[10, 20, 30]

Time Complexity: O(N)

Related Methods: filter()

Comparison:

map() vs list comprehension: list comprehensions are often preferred for readability and slightly better speed in modern Python.

Remember: Returns a lazy map iterator object; consume with list() or iterate directly.
filter() with Lambda
Return: filter object

Filter out items in an iterable using a truthy conditional lambda predicate.

Used In: Cleaning invalid data rows, selecting active logs.

Syntax signature:filter(lambda item: cond, iterable)
Code snippet:
python
lst = [10, 15, 20]
res = filter(lambda x: x % 2 == 0, lst)
print(list(res))
Expected Output:[10, 20]

Time Complexity: O(N)

Related Methods: map()

Remember: Returns a lazy iterator; filters out all items where the lambda evaluates to False.
sorted(key=lambda ...)
Return: List

Sort collections of records using a custom key accessor function.

Used In: Ordering tables by custom columns, sorting key metrics.

Syntax signature:sorted(iterable, key=lambda x: x['key'])
Code snippet:
python
users = [{'name': 'A', 'sal': 30}, {'name': 'B', 'sal': 10}]
print(sorted(users, key=lambda x: x['sal']))
Expected Output:[{'name': 'B', 'sal': 10}, {'name': 'A', 'sal': 30}]

Time Complexity: O(N log N)

Related Methods: list.sort()

Remember: Original collection remains untouched; returns a brand new sorted list.
reduce() with Lambda
Return: T

Cumulative folding operation running left-to-right across elements.

Used In: Aggregating metrics, cumulative multiplications.

Syntax signature:functools.reduce(lambda acc, val: expr, iterable)
Code snippet:
python
from functools import reduce
lst = [1, 2, 3, 4]
val = reduce(lambda acc, x: acc * x, lst)
print(val)
Expected Output:24

Time Complexity: O(N)

Related Methods: sum(), math.prod()

Remember: Requires importing from functools module. An optional initializer argument can be passed.
Lambda vs Normal Functions
Return: None

Compare anonymous lambdas with standard def statements.

Used In: Selecting coding styles for readability.

Syntax signature:N/A
Code snippet:
python
# Lambda: f = lambda x: x + 1
# Normal: def f(x): return x + 1
Expected Output:N/A

Time Complexity: O(1)

Remember: Always use normal functions if you need docstrings, multiple statements, exception blocks, or type annotations.