What is Python map() function?
The purpose of the Python map function is to apply the same procedure to every item in an iterable data structure. Iterable data structures can include lists, generators, strings, etc. In a very basic example, the map can iterate over every item in a list and apply a function to each item.
Python borrows the concept of the map from the functional programming domain. It is an inbuilt function that is used to apply the function on all the elements of specified iterable and return map objects. the map can also be used in situations like calling a particular method on all objects stored in a list which change the state of the object.
Also Read: Fibonacci series in Python
Syntax of Python map function
Python map() function syntax is:
map(function, iterable, ...)
Parameters of Python map function
function : It is a function to which map passes each element of given iterable. iterable : It is a iterable which is to be mapped.
NOTE: You can pass one or more iterable to the map() function.
We can pass multiple iterable arguments to map() function, in that case, the specified function must have that many arguments. The function will be applied to these iterable elements in parallel. With multiple iterable arguments, the map iterator stops when the shortest iterable is exhausted.
First argument: A function
The map function accepts a function as the first argument. When we think about a function in Python, we automatically think about the def
keyword, but the map function does not only accept functions created by the user using def
keyword but also built-in and anonymous functions, and even methods.
Second argument: An iterable
When we think about an iterable We automatically think about lists, but iterables are much more than lists. An iterable is an object with a countable number of values that can be iterated for example using a for loop, Sets, tuples, dictionaries are iterables as well, and they can be used as the second argument of the map function.
Examples: Python map() with string
def to_upper_case(s):
return str(s).upper()
It’s a simple function that returns the upper case string representation of the input object.
I am also defining a utility function to print iterator elements. The function will print iterator elements with white space and will be reused in all the code snippets.
def print_iterator(it):
for x in it:
print(x, end=' ')
print('') # for new line
Let’s look at the map() function example with different types of iterables.
# map() with string
map_iterator = map(to_upper_case, 'abc')
print(type(map_iterator))
print_iterator(map_iterator)
Output:
<class 'map'>
A B C
Python map() with tuple
# map() with tuple
map_iterator = map(to_upper_case, (1, 'a', 'abc'))
print_iterator(map_iterator)
Output:
1 A ABC
Python map() with list
map_iterator = map(to_upper_case, ['x', 'a', 'abc'])
print_iterator(map_iterator)
Output:
X A ABC
Example: Python map() function with lambda function
In the map() function along with iterable sequence, we can also the lambda function. Let’s use a lambda function to reverse each string in the list as we did above using a global function, Python
listOfStr = ['hi', 'this' , 'is', 'a', 'very', 'simple', 'string' , 'for', 'us']
# Reverse each string in the list using lambda function & map()
modifiedList = list(map(lambda x : x[::-1], listOfStr))
print('Modified List : ', modifiedList)
Output:
Modified List : ['ih', 'siht', 'si', 'a', 'yrev', 'elpmis', 'gnirts', 'rof', 'su']
It iterates over the list of string and applies lambda function on each string element. Then stores the value returned by lambda function to a new sequence for each element. Then in last returns the new sequence of reversed string elements.
Example: Passing multiple arguments to map() function in Python
The map() function, along with a function as an argument can also pass multiple sequences like lists as arguments. Let’s see how to pass 2 lists in
map() function and get a joined list based on them.
Suppose we have two lists i.e.
list1 = ['hi', 'this', 'is', 'a', 'very', 'simple', 'string', 'for', 'us']
list2 = [11,22,33,44,55,66,77,88,99]
Now we want to join elements from list1 to list2 and create a new list of the same size from these joined lists i.e. new lists should be like this,
['hi_11', 'this_22', 'is_33', 'a_44', 'very_55', 'simple_66', 'string_77', 'for_88', 'us_99']
Conclusion
- The map function has two arguments (1) a function, and (2) an iterable.
- Applies the function to each element of the iterable and returns a map object.
- The returned map object can be easily converted in another iterable using built-in functions.
Read More about Python Maps
If you didn’t find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.