In this tutorial, we will talk about the python map function. Python map function is one of the most used functions. We will cover everything about python map function in this tutorial like how python map will work with different inbuilt data structures, what is the role of python map function. Let’s see one by one, and discuss everything which will be faced in this tutorial.
What is the python map function?
The Python map() method applies a function to each item of an iterator that is provided as input. For instance, an iterator can return an iterable map object and be a list, tuple, set, dictionary, or string. Map() is a built-in function in Python.
Syntax:
map(function, iterator1,iterator2 ...iteratorN)
Parameters
Here are two crucial for python map function…
function: A necessary function that must be provided to map; it will be applied to each item in the iterator.
Iterator: A required iterable object. It could be a list, tuple, etc. The map() function accepts multiple iterator objects as arguments.
Return Value
The map() method will apply the specified function to each item in the iterator and produce a tuple, list, or other iterable map object.
How does the python map() function work?
The iterable object and a function are the two inputs for the map() function. The function passed to map() is a standard function, and it will loop through each value in the supplied iterable object.
Consider, for instance, that you want to determine the square of each integer in a list of numbers.
We require the function that returns the square of the input in order to obtain the result. The job will involve the following:
def square(n):
return n*n
The following is a list of the things we wish to locate the square for:
my_list = [2,3,4,5,6,7,8,9]
Let’s now utilise the built-in map() Python method to get the square of each item in my list.
Here is the final code:
def square(n):
return n*n
my_list = [2,3,4,5,6,7,8,9]
updated_list = map(square, my_list)
print(updated_list)
print(list(updated_list))
Output:
A map object at 0x0000002C59601758 is presented on the screen as the map() function’s result, as seen in the output.
To obtain the desired result, you must use a for-loop or the list() method to iterate the map’s output. In the code that displays the values inside the provided list, I’ve used list().
Consequently, we are able to obtain the square of each number using the map() function.
Map was given the following list: [2,3,4,5,6,7,8,9] and after applying the square() function, the output of map() was [4, 9, 16, 25, 36, 49, 64, 81].
All of the entries in the list are subject to the square() function thanks to the python map function. For instance, my_list variable updates the list with each number’s square. The updated_list variable holds the out.
Using python map() with built-in functions
The built-in map() function in Python can be used with other built-in functions to create new functions. We’ll use the built-in Python round() method in the example to round the supplied values.
Example:
I have a list called my_list = [2.6743,3.63526,4.2325,5.9687967,6.3265,7.6988,8.232,9.6907] .
Each item’s rounded value from the list must be provided. Round() will be used as the function to map().
my_list = [2.6743,3.63526,4.2325,5.9687967,6.3265,7.6988,8.232,9.6907]
updated_list = map(round, my_list)
print(updated_list)
print(list(updated_list))
Output:
The list is processed by the round() function, which rounds all of the elements values as displayed in the output.
Using python map() with a string as an iterator
Map() can also be used on a string. Since a string in Python functions like an array, using it inside the map() is simple.
The function myMapFunc() in the example takes care of changing the provided string to uppercase. The map() method receives the function myMapFunc(). By sending the string to myMapFunc(), the map function will handle changing the given string to uppercase.
Code Implementation
def myMapFunc(s): return s.upper() my_str = "welcome to prepbytes!" updated_list = map(myMapFunc, my_str) print(updated_list) for i in updated_list: print(i, end="")
Output:
Using python map() with list of Numbers
Each number in the list will be multiplied by 10 in order to deal with the list in map().
We will use the following list: [2,3,4,5,6,7,8,9]. The function myMapFunc() handles multiplying 10 to the integer provided. Along with the list, the function for mapping is provided.
Code Implementation:
def myMapFunc(n): return n*10 my_list = [2,3,4,5,6,7,8,9] updated_list = map(myMapFunc, my_list) print(updated_list) print(list(updated_list))
Output:
Each number in the list has been multiplied by 10 in the result that is visible.
Using python map() with Tuple
Python objects with items surrounded in round brackets and separated by commas are known as tuples. We’ll use a tuple containing string values in the example. The function we’ll employ will change the values from lowercase to uppercase.
Code Implementation:
def myMapFunc(n): return n.upper() my_tuple = ('php','java','python','c++','c') updated_list = map(myMapFunc, my_tuple) print(updated_list) print(list(updated_list))
Output:
The result is a tuple that has all the values changed to uppercase.
Using python map() with Dictionary
Curly brackets({}) are used to generate a dictionary in Python. You can use the dictionary inside the map() function because it is an iterator. Now let’s use the map() function’s dictionary as an iterator.
The following example demonstrates how the dictionary iterator in a map() operates
def myMapFunc(n): return n*10 my_dict = {2,3,4,5,6,7,8,9} finalitems = map(myMapFunc, my_dict) print(finalitems) print(list(finalitems))
Output:
Using python map() with Set
Set in Python is an unordered collection of items in curly brackets(()). Since set() is also an iterator, you can make use of it inside map() function.
Here is a working example of using set as an iterator inside map()
def myMapFunc(n): return n*10 my_set = {2,3,4,5,6,7,8,9} finalitems = map(myMapFunc, my_set) print(finalitems) print(list(finalitems))
Output:
Using python map() with Lambda function
A set in Python is a group of elements enclosed in curly brackets ({}). You can use set() inside the map() function because it is also an iterator.
Here is a working illustration of how to use the set inside of map() as an iterator.
Code Implementation
def myMapFunc(n): return n*10 my_set = {2,3,4,5,6,7,8,9} finalitems = map(myMapFunc, my_set) print(finalitems) print(list(finalitems))
Output:
Using Multiple Iterators inside python map function
Here, we will see multiple iterators inside the python map function using a single function inside it.
Example 1: Passing two list iterators to map()
You can send multiple iterators, such as a list, a tuple, etc., to the map() function at once.
Suppose you want to add two lists, for instance. Using the map() function, the same thing is possible. my_list1 and my_list2 are the two lists we will employ.
The first item in my_list1 is added to the first item in my_list2 in the example below. MyMapFunc() accepts elements from my_list1 and my_list2 and returns their sum.
Here is a working example of using the map() method to combine two lists.
Code Implementation
def myMapFunc(list1, list2): return list1+list2 my_list1 = [2,3,4,5,6,7,8,9] my_list2 = [4,8,12,16,20,24,28] updated_list = map(myMapFunc, my_list1,my_list2) print(updated_list) print(list(updated_list))
Output:
Example 2: Passing one Tuple and a list iterator to map()
In the map() function, we’ll utilize a list and a tuple iterator. The function is passed to the map; myMapFunc() will retrieve the list and tuple items. An underscore(_) will be used to join the items. The working illustration is displayed below.:
def myMapFunc(list1, tuple1):
return list1+"_"+tuple1
my_list = ['a','b', 'b', 'd', 'e']
my_tuple = ('PHP','Java','Python','C++','C')
updated_list = map(myMapFunc, my_list,my_tuple)
print(updated_list)
print(list(updated_list))
Output:
Conclusion
- The built-in method map() in Python applies a function to each item in an input iterator. An iterator returns an iterable map object and can, for instance, be a list, a tuple, a string, etc.
- The map() method will apply the specified function to each item in the iterator and produce a tuple, list, or other iterable map object.
- The built-in map() function in Python can be used with other built-in functions to create new functions.
- Python objects with items surrounded in round brackets and separated by commas are known as tuples. A tuple with string values will be used in the example. The function we’ll employ will change the values from lowercase to uppercase.
- Curly brackets({}) are used to generate a dictionary in Python. You can use the dictionary inside the map() function because it is an iterator.
- A set in Python is a group of elements enclosed in curly brackets (()). You can use set() inside the map() function because it is also an iterator.
- In Python, anonymous functions are created using lambda expressions (also known as lambda forms). Therefore, if you wish to utilise lambda inside the map, you must use the lambda keyword ().
- You can use the map() function to communicate several iterators, such as a list or tuple.