Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Python map() function

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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.) Syntax :

map(fun, iter)

Parameters :

fun : It is a function to which map passes each element of given iterable. iter : It is a 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) .   CODE 1 

Python3




# 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))


Output :

[2, 4, 6, 8]

  CODE 2 We can also use lambda expressions with map to achieve above result.

 

Python3




# Double all numbers using map and lambda
 
numbers = (1, 2, 3, 4)
result = map(lambda x: x + x, numbers)
print(list(result))


Output :

[2, 4, 6, 8]

  CODE 3 

Python3




# 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))


Output :

[5, 7, 9]

  CODE 4 

Python3




# 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), as the resulting list test contains n sublists, each of which contains a single character. 

using 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.

Time complexity analysis:

The map function applies the double_even function to each element of the list. The time complexity of the map function is O(n), where n is the number of elements in the list. The time complexity of the double even function is constant, O(1), since it only performs a single arithmetic operation and a comparison. Therefore, the overall time complexity of the program is O(n).

Space complexity analysis: The program uses a list to store the result of the map function, so the space complexity is proportional to the number of elements in the list. In the worst case, if all elements are even, the resulting list will have the same number of elements as the input list. Therefore, the space complexity is O(n) in the worst case.

Python3




# 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]


Output

[1, 4, 3, 8, 5]

Time complexity: O(n), where n is the length of the input list. The map() function applies the double_even() function to each element in the list, which takes constant time. Therefore, the overall time complexity is proportional to the length of the input list.

Auxiliary space complexity: O(n), where n is the length of the input list. The map() function creates a new list to store the output, which takes up space proportional to the length of the input list. Therefore, the auxiliary space complexity is also proportional to the length of the input list.


My Personal Notes arrow_drop_up
Last Updated : 21 Mar, 2023
Like Article
Save Article
Similar Reads
Related Tutorials