We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Anonymous Functions

Anonymous functions have no name, and in Python, they're called lambda functions after lambda calculus. Here's a lambda function that takes a single argument x and returns the result of x + 1:

lambda x: x + 1

Notice that the expression x + 1 is returned automatically, no need for a return statement. Compare that to how you'd normally write a function:

def add_one(x: int) -> int:
    return x + 1

Because functions are just values, we can assign the function to a variable named add_one:

from collections.abc import Callable

add_one: Callable[[int], int] = lambda x: x + 1
print(add_one(2))
# 3

Lambda functions might look scary, but they're still just functions. Because they simply return the result of an expression, they're often used for small, simple evaluations. Here's an example that uses a lambda to get a value from a dictionary:

get_age: Callable[[str], int | str] = lambda name: {
    "lane": 29,
    "hunter": 69,
    "allan": 17,
}.get(name, "not found")
print(get_age("lane"))
# 29

Assignment

Complete the file_type_getter function. This function accepts a list of tuples, where each tuple contains:

  1. A "file type" (e.g. "code", "document", "image", etc.)
  2. A list of associated file extensions (e.g. [".py", ".js"] or [".docx", ".doc"])

The function returns a function for looking up the file type of a given file extension.

    For example, if given the following list of tuples:

    # list of tuples
    file_extension_tuples: list[tuple[str, list[str]]] = [
        ("document", [".doc", ".docx"]),
        ("image", [".jpg", ".png"]),
    ]
    
    # resulting dictionary
    file_extensions_dict: dict[str, str] = {
        ".doc": "document",
        ".docx": "document",
        ".jpg": "image",
        ".png": "image",
    }