Python map() function
Last Updated :
19 Jun, 2024
map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.)
Python map() Function Syntax
Syntax: map(fun, iter)
Parameters:
- fun: It is a function to which map passes each element of given iterable.
- iter: It is iterable which is to be mapped.
NOTE: You can pass one or more iterable to the map() function.
Returns: Returns a list of the results after applying the given function to each item of a given iterable (list, tuple etc.)
NOTE : The returned value from map() (map object) then can be passed to functions like list() (to create a list), set() (to create a set) .
map() in Python Examples
Demonstration of map() in Python
In this example, we are demonstrating the map() function in Python.
Python
# Python program to demonstrate working
# of map.
# Return double of n
def addition(n):
return n + n
# We double all numbers using map()
numbers = (1, 2, 3, 4)
result = map(addition, numbers)
print(list(result))
map() with Lambda Expressions
We can also use lambda expressions with map to achieve above result. In this example, we are using map() with lambda expression.
Python
# Double all numbers using map and lambda
numbers = (1, 2, 3, 4)
result = map(lambda x: x + x, numbers)
print(list(result))
Add Two Lists Using map and lambda
In this example, we are using map and lambda to add two lists.
Python
# Add two lists using map and lambda
numbers1 = [1, 2, 3]
numbers2 = [4, 5, 6]
result = map(lambda x, y: x + y, numbers1, numbers2)
print(list(result))
Modify the String using map()
In this example, we are using map() function to modify the string. We can create a map from an iterable in Python.
Python
# List of strings
l = ['sat', 'bat', 'cat', 'mat']
# map() can listify the list of strings individually
test = list(map(list, l))
print(test)
Output[['s', 'a', 't'], ['b', 'a', 't'], ['c', 'a', 't'], ['m', 'a', 't']]
Time complexity: O(n), where n is the number of elements in the input list l.
Auxiliary space: O(n)
if Statement with map()
In the example, the double_even() function doubles even numbers and leaves odd numbers unchanged. The map() function is used to apply this function to each element of the numbers list, and an if statement is used within the function to perform the necessary conditional logic.
Python
# Define a function that doubles even numbers and leaves odd numbers as is
def double_even(num):
if num % 2 == 0:
return num * 2
else:
return num
# Create a list of numbers to apply the function to
numbers = [1, 2, 3, 4, 5]
# Use map to apply the function to each element in the list
result = list(map(double_even, numbers))
# Print the result
print(result) # [1, 4, 3, 8, 5]
Time complexity: O(n)
Auxiliary complexity: O(n)
Python map() function – FAQs
How to use Python map function with lambda?
You can use map() with a lambda function to apply the lambda function to each element of an iterable. Here’s an example:
numbers = [1, 2, 3, 4, 5]
squared = map(lambda x: x**2, numbers)
In this example, map() applies the lambda function lambda x: x**2 to each element in the numbers list, producing an iterator (map object) containing the squared values.
How to convert Python map()
function to list?
You can convert the map() object to a list using list(). For example:
squared_list = list(squared)
This converts the map object squared (which contains the squared values of numbers) into a list squared_list.
What is a simple Python map()
function example?
A simple example using map() without lambda:
def double(x):
return 2 * x
numbers = [1, 2, 3, 4, 5]
doubled = map(double, numbers)
Here, map() applies the double function to each element in numbers, producing an iterator (map object) containing the doubled values.
How to use Python map()
function with multiple arguments?
You can use map() with multiple iterables and a function that accepts multiple arguments. For example:
numbers1 = [1, 2, 3]
numbers2 = [4, 5, 6]
added = map(lambda x, y: x + y, numbers1, numbers2)
Here, map() applies the lambda function lambda x, y: x + y to pairs of elements from numbers1 and numbers2, producing an iterator (map object) containing the sums.
What happens when Python map()
function is applied over a list?
When map()
is applied over a list, it returns a map
object, which is an iterator that yields results lazily as they are needed. To get the results as a list, you typically convert the map
object using list()
.
Please Login to comment...