Syntax of Map function in Python
In this tutorial, we will understand what the map function in Python is meant. Further, we will see the syntax to be used for the same in python language. We will understand the map function with the aid of codes and examples.
Understanding the map function
A map() function is a function that yields a map entity (which is an iterator) of the outcomes after applying the given function to each item of a given iterable (list, tuple, set, etc.)
Syntax:
map (func, iter)
Description of Parameters:
func: It refers to a function to which a map is passed each element of a given iterable.
Iter is an iterable (list, tuple, etc.) that is to be mapped.
Here, more than one iterable is allowed to pass to the function.
Return value:
The map function returns a list of the outcomes the given function is applied to each item of a given iterable (list, tuple, etc.).
The value returned from map() function (map object) can be further sent to functions such as list() to create a list, set() to create a set.
Illustration 1:
Let us start with the basic program to get an overview of the map in Python.
Here, the numbers are getting doubled up using the map function.
# Python program to show the working of a map in Python
# Returns double of m
def add(m):
return m + m
# We double all numbers using map()
nos = (16, 26, 43, 64)
outcome = map(add, nos)
print(list(outcome))
Output:

Illustration 2:
Here is the other example to understand the map even better.
# Python program to show the working of a map in Python
# Double each number by taking map and lambda functions into account
nos = (1, 2, 3, 4)
outcome = map(lambda x: x + x, nos)
print(list(outcome))
Output:

Illustration 3:
Here is the program to understand the map's working by using lambda along with the map function.
# Python program to understand the map in Python
# Adding two lists using map and lambda functions
nos1 = [11, 12, 13, 10, 18]
nos2 = [14, 15, 16, 17, 19]
outcome = map (lambda x, y: x + y, nos1, nos2)
print(list(outcome))
Output:

Illustration 4:
We will now understand the working of the Map function with the list iterator.
# Online Python-3 Compiler (Interpreter)
# Python program to understand the map in Python
# List of strings
lst = ['sun', 'mon', 'tues', 'wed', 'thurs','fri', 'sat',]
# map() function can listify the list of strings one by one
tst = list(map(list, lst))
print(tst)
Output:

Illustration 5:
# Python program to understand the map in Python
def func1(a):
return len(a)
z = map (func1, ('apple', 'banana', 'cherry', 'guava', 'watermelon'))
print(z)
# converting the map into a list to enhance the readability
print(list(z))
Output:

Illustration 6:
We will now understand the working of the Map function through mathematical function – math.sqrt.
#Python program to understand the map in Python
import math
nos = [144, 36, 4, 49, 81, 121, 64]
z = list (map (math.sqrt, nos) )
print(z)
Output:

Illustration 7:
We will now understand the working of the Map function with the tuple iterator.
#python program to understand the working of map
def illustration(s):
return s.upper()
tuple_example = ('It', 'is', 'available', 'on', 'tutorials', 'and', 'example', 'website')
upd_tup = map(illustration, tuple_example)
print(upd_tup)
print(tuple(upd_tup))
Output:

Illustration 8:
We will now understand the working of the Map function with the dictionary iterator.
#Python program to understand the working of map
subject_dict = {'a': 'History', 'b': 'anthropology', 'c': 'polity', 'd': 'hindi', 'e': 'geography', 'f': 'english'}
print ('The original dictionary: ')
print (subject_dict)
# Adding an underscore '_' at the end of each value
subject_dict = dict (map (lambda x: (x[0], x[1] + '_'), subject_dict.items() ))
print ('The modified dictionary is: ')
print(subject_dict)
Output:

Illustration 9:
We will now understand the working of the Map function with the set iterator.
def example_set(z):
return z%3
set_eg = {133, 172, 86, 90, 84, 58, 97}
upd_itms = map(example_set, set_eg)
print (upd_itms)
print (set (upd_itms))
Output:

Summary
In this module, we learned what a map function in Python is. We learned the syntax of the map function, which consist of two parameters one is the function, and the other is iterable, which can be anything set, dictionary, tuple, list, etc.
Further, we saw the map function in depth through the aid of various example codes written in the Python compiler.