Python map
last modified July 6, 2020
Python map tutorial presents the Python built-in map()
function.
Python map() function
Python map()
built-in function applies the given
function on every item of iterable(s) and returns an iterator object.
map(function, iterable, ...)
It is possible to pass more than one iterable to the map()
function.
The function must take as many parameters as there are iterables.
Python map example
The following example uses Python map()
on
a list of integers.
#!/usr/bin/python3 def square(x): return x * x nums = [1, 2, 3, 4, 5] nums_squared = map(square, nums) for num in nums_squared: print(num)
We define a list of integers and apply the square()
function on each element of the list with map()
.
def square(x): return x * x
The square()
function squares its parameter.
nums = [1, 2, 3, 4, 5]
We define a list of integers.
nums_squared = map(square, nums)
The map()
function applies the square()
function on each element of the nums
list.
for num in nums_squared: print(num)
We loop over the returned iterable and print the elements.
$ ./python_map.py 1 4 9 16 25
This is the output.
Python map equivalent
The following example shows a custom equivalent to Python 3 map()
function.
#!/usr/bin/python3 def square(x): return x * x def mymap(func, iterable): for i in iterable: yield func(i) nums = [1, 2, 3, 4, 5] nums_squared = mymap(square, nums) for num in nums_squared: print(num)
The mymap()
does the same thing as Python 3
map()
.
Python map with lambda
The next example creates an anonymous function inside
map()
with lambda
operator.
#!/usr/bin/python3 nums = [1, 2, 3, 4, 5] nums_squared = map(lambda x: x*x, nums) for num in nums_squared: print(num)
The code example squares the elements of a list with
map()
and anonymous function created with
lambda
.
Python map with multiple iterables
We have mentioned earlier that we can pass multiple iterables
into map()
.
#!/usr/bin/python3 def multiply(x, y): return x * y nums1 = [1, 2, 3, 4, 5] nums2 = [6, 7, 8, 9, 10] mult = map(multiply, nums1, nums2) for num in mult: print(num)
In the code example have two iterables holding integers. The values from both iterables are multiplied.
def multiply(x, y): return x * y
The function must take two parameters since there are two iterables passed
to map()
.
$ ./python_map_iterables.py 6 14 24 36 50
This is the output.
Python map multiple functions
In the next example, we show how to use multiple functions
in Python map()
.
#!/usr/bin/python3 def add(x): return x + x def square(x): return x * x nums = [1, 2, 3, 4, 5] for i in nums: vals = list(map(lambda x: x(i), (add, square))) print(vals)
In the example, we apply add()
and square()
functions on the list of integer values.
for i in nums: vals = list(map(lambda x: x(i), (add, square))) print(vals)
We go through the elements in the for loop. In each cycle, we
create a list of two values, which are computed by applying
the add()
and square()
functions on
the value.
$ ./python_map_multiple_funcs.py [2, 1] [4, 4] [6, 9] [8, 16] [10, 25]
The first value is formed by addition, the second one by multiplication.
Python list comprehension
The functionality of Python map()
can be achieved with Python list
comprehensions as well.
#!/usr/bin/python3 def square(x): return x * x nums = [1, 2, 3, 4, 5] nums_squared = [square(num) for num in nums] for num in nums_squared: print(num)
The example creates a list of squared values from a list of integers with Python list comprehension.
In this tutorial, we have worked with the Python map()
function.
List all Python tutorials.