Anonymous Functions

Anonymous functions have no name, and in Python, they're called lambda functionsarrow-up-right after lambda calculusarrow-up-right. 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 expressionarrow-up-right x + 1 is returned automatically, no need for a return statement. And because functions are just values, we can assign the function to a variable named add_one:

add_one = 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 = 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"])

First, use loops to create a dictionary that maps each file extension to its corresponding file type, based on the input tuples. For example, the resulting dictionary might be:

Next, return a lambda function that accepts a string (a file extension) and returns the corresponding file type. If the extension is not found in the dictionary, the lambda function should return "Unknown". I used the .getarrow-up-right dictionary method to handle this.

Solution

Last updated