Skip to content

Lambda functions

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. They must contain only one expression, so they are not best suited for functions with control flow statements.

Misused

Lambda functions help to write is just one line, code that would require a bunch of lines. It can make the code less readable. They are very useful, but overuse can make the code difficult to understand.

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

Functions like map() can be used with lambda functions. In that case, map() will 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 map(lambda d: d[field_to_be_extracted], content)

Apply lambda functions for sorting data

Lambda functions can be used for sorting dada 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

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))