Programming Languages

What is the purpose of the map function in Python?

Easy
4
Added
The map function applies a given function to each item of an iterable (like a list) and returns a map object (an iterator).

Solution Code

Programming Languages
def square(x):
    return x * x

numbers = [1, 2, 3, 4, 5]
squared = map(square, numbers)
print(list(squared))
Explanation
This code snippet demonstrates how to use the map function to apply a function to each item in a list.

Guided Hints

Define a function to apply
Use map to apply the function to the iterable
Convert the map object to a list to see the results