Map Function in Python
Apr 5, 2024
The map() function allows you to perform the same operation on each item in a collection (like a list) without having to write a loop. It returns a new map object that can be converted into a List or Tuple.
The map() function doesn’t change the data types of the elements in the iterable itself; rather, it creates a new iterable with the transformed values.
Here’s an example of using map() with different data types:
def square(x):
return x ** 2
numbers = [1, 2, 3, 4, 5]
squared_numbers = map(square, numbers)
print(list(squared_numbers))
# Output: [1, 4, 9, 16, 25]