Search⌘ K
AI Features

Creating Anonymous Functions

Explore how to create anonymous functions in Python using lambdas and closures. Understand the use of the map function for applying operations to list elements, and learn the advantages of closures for creating flexible, reusable functions beyond simple expressions.

Anonymous functions

Now we will look at various ways that closures can be used, starting with using closures to create anonymous functions.

A simple introduction to map

The map function is a built-in Python function. In its simplest form, it accepts a function object and a sequence (e.g., a list). It applies the function to each element of the list.

Python 3.8
a = [2.2, 5.6, 1.9, 0.1]
b = map(round, a)
# print(b)
print(list(b)) # [2, 6, 2, 0]

In this example, we apply the round function to every element ...