In this Python guide, we are going through the Python map() function to execute a specific function for each item for an iterable. map function in Python is the most important function when you want to execute a function for each item of an iterable.
In the previous Python built-in functions tutorials, we have seen various functions along with examples.
Headings of Contents
What is the Python map() function?
The Python map() function is a built-in function. map() function in Python is used to apply a specific function on each item of an iterable ( list, tuple, string, etc) and return a map object and at the end, the map object can convert into a list using the list() function.
Syntax
The syntax of the map() function in python is:-
map(function, iterable)
Parameter
map function in Python accepts two parameters.
- function:- Required, the function executed for each item.
- iterable:- Required. A sequence, collection, or an iterator object.
Note:- You can pass more than one iterable to the map() function
Python map() function examples
Let’s understand the Python map() function along with various examples so that you can get more clarity with that.
Example: Converting each integer item of the list into a string
lst = [2, 4, 6, 8, 10, 12]
result = map(str, lst)
print("List after converting item to string:- ", list(result))
Output
List after converting item to string:- ['2', '4', '6', '8', '10', '12']
Example: Converting each item of the tuple to their Unicode character
Convert a tuple of integers into a set of strings using the map function.
x = ("A", "b", "C", "d", "E")
y = map(ord, x)
print(list(y))
Output
[65, 98, 67, 100, 69]
map() function with a custom function
Python map() function is also responsible for accepting custom functions which are not come with Python by default.
Example: Square each item of the list using the map() function
In this example, we will pass a list having some integer value to the map() function and map() function apply a function to each item of the list and calculate the square of a particular item and finally return a list.
def calculate_square(n):
"""
:param n: n refers to each item of the list
:return: return square n * n
"""
return n * n
lst = [2, 4, 6, 8, 10, 12]
result = map(calculate_square, lst)
print("Map Object:- ", result)
print("Square of list items", list(result))
Output
Map Object:- <map object at 0x000002BE2D57ACE0>
Square of list items [4, 16, 36, 64, 100, 144]
Example: Calculating the length of string items of the list
Calculate the length of each string item of the tuple using the map() function.
def calculate_length(n):
"""
:param n: n refers to the each item of the list
:return: length of n
"""
return len(n)
lst = ("Java", "Python", "C++", "Dot Net")
result = map(calculate_length, lst)
print("Map Object:- ", result)
print("Square of list items", list(result))
Output
Map Object:- <map object at 0x000001F4E54FAC50>
Square of list items [4, 6, 3, 7]
Example: Using map() function with a lambda function
In this example, I am trying to add 10 to each item of the tuple. Here I am using the Python lambda function. You can explore more about the Python lambda function.
lst = [2, 4, 6, 8, 10, 12]
result = map(lambda x: x + 10, lst)
print("New List:- ", list(result))
Output
New List:- [12, 14, 16, 18, 20, 22]
Example: Passing more than one iterables to the map()
In this example, I am passing two iterable ( lst1 and lst2 ) to the function map () function, and the map() function uses an addition function which is responsible for adding the sum of the corresponding items of the iterable
def addition(x, y):
"""
:param x: x refer to item of the first iterable
:param y: y refer to item of the second iterable
:return: return sum of x and y
"""
return x + y
# first iterable
lst1 = [12, 14, 16, 18]
# second iterable
lst2 = [20, 40, 50, 60]
result = map(addition, lst1, lst2)
print("Result:- ", list(result))
Output
Result:- [32, 54, 66, 78]
Conclusion
In this tutorial, you have learned all about the Python map function to execute a specific function for each item of an iterable.
This is the best built-in function when you want to perform a specific operation on each item of iterable.
If you like this article, please share and keep visiting for further python built-in functions.
Python built-in functions
For more information:- Click Here