Skip to content

Lambda functions

Quality Score

Overall Score: 9.2/10 ⭐ Excellent

  • Technical Accuracy: 28/35
  • Code Quality: 25/25
  • Educational Value: 24/25
  • Documentation: 15/15

Last reviewed: June 22, 2026

Lambda expressions are ideally used when we need to do something simple and are more interested in getting the job done quickly rather than formally naming the function.

Lambda expressions are also known as anonymous functions.

Lambda functions behave like normal functions declared with the def keyword. They are useful when you want to define a small function concisely. This limit the usage to single expression, without statements and without type hints. Usually, best used is for sorting, filtering or mapping data. Do not use more than one line or expression, that is consider a bad practice.

Lambda functions should not be used for complex logic, when debugging/observability is required or reusability is a concern. In those cases, it is better to use a normal function declared with the def keyword.

Performance and readability

Lambda functions have no impact on performance compared to normal methods (def functions). Well written, lambda functions can be more readable than normal functions, especially when the function is simple and context is clear. If readability is a concern, it is better to use a normal function.

Use with functions like map(), max(), etc

Functions like map can be used with lambda functions. In that case, map will apply the lambda function to every element of a given iterable (list, tuple, etc.).

For example:

src.advanced.lambda_functions.get_list_of_fields_from_a_list_dict(content, field_to_be_extracted)

Extract and return a given field from a list of dictionaries.

Parameters:

Name Type Description Default
content list[dict[str, str]]

list of dictionaries.

required
field_to_be_extracted str

field to be extracted.

required

Returns:

Type Description
list[str]

list[str]: list of the values extracted.

Source code in src/advanced/lambda_functions/lambda_functions.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def get_list_of_fields_from_a_list_dict(
    content: list[dict[str, str]],
    field_to_be_extracted: str,
) -> list[str]:
    """Extract and return a given field from a list of dictionaries.

    Parameters:
        content: list of dictionaries.
        field_to_be_extracted: field to be extracted.

    Returns:
        list[str]: list of the values extracted.
    """
    return list(map(lambda d: d[field_to_be_extracted], content))

Apply lambda functions for sorting data

Lambda functions can be used for sorting data in list, dict, etc.

src.advanced.lambda_functions.sort_a_list_of_dict_by_a_field(content_to_be_sorted, sorted_by, asc=True)

Sort a list of dict by a given field.

Parameters:

Name Type Description Default
content_to_be_sorted list[dict]

list of dict to be sorted.

required
sorted_by str

field to be used for sorting.

required
asc bool

if false, it is sort in descending order. By default, it is true, ascending order.

True

Returns:

Type Description
list[dict[str, str]]

list[dict[str, str]]: content sorted.

Source code in src/advanced/lambda_functions/lambda_functions.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def sort_a_list_of_dict_by_a_field(
    content_to_be_sorted: list[dict],
    sorted_by: str,
    asc: bool = True,
) -> list[dict[str, str]]:
    """Sort a list of dict by a given field.

    Parameters:
        content_to_be_sorted: list of dict to be sorted.
        sorted_by: field to be used for sorting.
        asc: if false, it is sort in descending order. By default, it is true,
            ascending order.

    Returns:
        list[dict[str, str]]: content sorted.
    """
    return sorted(
        content_to_be_sorted,
        key=lambda d: d[sorted_by],
        reverse=not asc,
    )

Pass a lambda function as parameter to a function

Lambda functions can be passed as parameters to other functions. This is useful when we want to customize the behavior of a function without having to define a new function.

src.advanced.lambda_functions.filter_by_applying_function_to_elements(func, elements)

Apply a function to a list of elements.

Parameters:

Name Type Description Default
func Callable

function to apply.

required
elements list[Any]

elements to apply the function.

required

Returns:

Type Description
list[Any]

list[Any]: the result of apply a function to a list of elements.

Source code in src/advanced/lambda_functions/lambda_functions.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def filter_by_applying_function_to_elements(
    func: Callable,
    elements: list[Any],
) -> list[Any]:
    """Apply a function to a list of elements.

    Parameters:
        func: function to apply.
        elements: elements to apply the function.

    Returns:
        list[Any]: the result of apply a function to a list of elements.
    """
    return list(filter(func, elements))

Lambda gotchas

One common issue is related to usage of lambda functions in loops, where the lambda function captures the variable by reference, not by value. This can lead to unexpected behavior when the lambda function is executed later.

# bad example: Lambda captures variable by reference
functions = []
for i in range(5):
    functions.append(lambda: i)

# good example: Use default argument to capture by value
functions = []
for i in range(5):
    functions.append(lambda i=i: i)

Another gotcha is related to late binding, where the lambda function uses the value of a variable at the time the function is called, not at the time the function is defined.

# bad example: Lambda uses variable value at call time
multiplier = 2
multiply = lambda x: x * multiplier

# good example: Use a regular function for clarity
def create_multiplier(multiplier):
    return lambda x: x * multiplier