Many a time while working on a list of items in Python, we may want to perform a specific operation or functionality on each and every item in the list to build a new iterable. For such an issue, the Python map() function comes into the picture. The Python map() function allows you to process and transform all the objects in an iterable without needing an explicit for loop, a method known as mapping. When you need to apply a transformation function to each item in an iterable and turn it into a new iterable, you can use the map() function.
Definition
- Python map() function applies a specific function to all the items of the iterable in order to create a new iterable.
- The Python map() method is a built-in function that provides a map object (an iterator) containing the results of applying the provided function in the program to each item of a given iterable (list, tuple, etc.)
Python map()
Because it maps every item in an input iterable to a new item in a resultant iterable, the action performed by map() is usually referred to as mapping. Map() does this by applying a transformation function to all of the elements in the input iterable. To better understand this, let us look at the syntax of the Python map() function.
SyntaxÂ
map(function, iterable, ….)
where,
function = transformation function in which map() passes every element of the iterable
iterable = lists, tuples, dictionary, sets, etc.
Note – We can pass multiple numbers of iterables to the map() function.
Return value from map()
The map() method outputs a list of results. The value produced by map() may subsequently be given to methods such as list() (to build a list), set() (to construct a set), tuple, and others.
Example 1: Working of the map()
Consider we have a tuple of numbers for which we need to find out the cube for every item in it. We use the map() function in order to pass the Cube function to each item in the iterable. And then we create a list() on the map() function to create a list iterable which will contain the cube values.
Example
# Python program to illustrate map() function
def Cube(n):
   return n*n*n
Â
num = (1, 2, 3, 4, 5)
result = map(Cube, num)
print(result)
# Converting map object to a list
cube_list = list(result)
print('List of Cubes:', cube_list)
Output
Example
# Python program to illustrate map() function with multiple iterators
def Fullname(FN, LN):
   return FN+"_"+LN
FN = ('John', 'Sam', 'Jean')
LN = ('Smith', 'Rodrigues', 'Paul')
result = map(Fullname, FN, LN)
print(result)
print(list(result))
Output
Example 2 – How to use lambda function with map()?
Instead of defining the function explicitly, the Python lambda function can be used. Python map() function also works on lambda functions. For tiny functions when we don’t want to define a new function, we can use lambda functions with map(). Let us look at the following example to understand how.
Example
# Python program to illustrate map() function with lambda
numbers = [1, 2, 3, 4, 5]
result = map(lambda x: x*10, numbers)
print(result)
print(set(result))
Output
Example 3: Passing Multiple Iterators to map() Using Lambda
In the below example, we have used items of 2 lists in the map() function and have calculated the divided value of each item using the lambda function.
Example
# Python program to illustrate map() function
num1 = [5, 10, 15, 20, 25, 30]
num2 = [5, 5, 5, 5, 5, 5]
result = map(lambda x, y: x/y, num1, num2)
print(result)
print(list(result))
Output
Example 4 – Using map() with a string iterator
On a string, we can also use a map(). Because a string behaves like an array in Python, we can easily use it within the map() function.
Example
# Python program to illustrate map() with a string iterator
def Func(i):
return i.upper()
txt = 'Welcome Home Jimmy'
result = map(Func, txt)
print(result)
for j in result:
print(j, end = '')
Output
Example 5 – Using map() with other built-in functions
It is not compulsory to always define a function and then use it in the map(). Python provides a wide range of built-in functions which can be utilized by just using the keyword. Few examples are –
Example
base = [2, 3, 4, 5]
index = [2, 3, 4, 5]Â
# pow function performs a^b operation
power = list(map(pow, base, index))
print(power)
Output
[4, 27, 256, 3125]
Example
numbers = [12.25641, 23.27353, 54.677462, 99.99999]
result = map(round, numbers)
print('Rounded off values:', list(result))
words = ['Python', 'Programming', 'Language']
length = map(len, words)
print('Length of words:', list(length))
Output
Rounded off values: [12, 23, 55, 100]
Length of words: [6, 11, 8]
Frequently Asked Questions
Q1. What is a map() in Python?
Many a time while working on a list of items in Python, we may want to perform a specific operation or functionality on each and every item in the list to build a new iterable. For this purpose, Python provides a map() built-in function. Python map() function applies a specific function to all the items of the iterable in order to create a new iterable.
Syntax
map(function, iterator 1, …., iterator n)
Q2. What is a map in Python with example?
Because it maps every item in an input iterable to a new item in a resultant iterable, the action performed by map() is usually referred to as mapping. Map() does this by applying a transformation function to all of the elements in the input iterable.
Example
# Python program to illustrate map() function
def Func(i):
   return i+5
numbers = (0, 2, 4, 6, 8, 10)
result = map(Func, numbers)
print(result)
print(list(result))
Output
Q3. How do I map a list in Python?
It is possible to map a list in Python. While passing the 2nd parameter i.e. the iterable to the map() function, we need to note that the iterator should be a list.
Example
# Python program to illustrate mapping of a list
def Func(i):
return i*10
numbers = [1, 3, 5, 7, 9]
result = map(Func, numbers)
print(result)
print(list(result))
Output
Q4. Does the lambda function work with map()?
Instead of defining the function explicitly, the Python lambda function can be used. Python map() function also works on lambda functions. For tiny functions when we don’t want to define a new function, we can use lambda functions with map(). Let us look at an example to understand the concept.
Example 1
# Python program to illustrate map() function with lambda function
names = ['Ricky', 'Tom', 'Kim', 'Lisa']
result = map(lambda x: x + ' - 8th Grade', names)
print(list(result))
Output
['Ricky - 8th Grade', 'Tom - 8th Grade', 'Kim - 8th Grade', 'Lisa - 8th Grade']
Example 2
num1 = [1, 3, 5, 7, 9]
num2 = [2, 4, 6, 8, 10]
result = map(lambda x, y: x+y, num1, num2)
print('Addition of 2 lists:', list(result))
Output
Addition of 2 lists: [3, 7, 11, 15, 19]
Leave a Reply