Python map Function

The Python map function applies the user given function to each item in an iterable such as list, tuple, etc. Next, this map function returns a list of the result values. In this section, we discuss how to use this map function in Python Programming with example. The basic syntax of the Python map function is

map(function_name, iterables,...)

Python map Values Example

In this Python map function example, we declared a function called addition. It adds a number to itself and returns that value. Next, we declared a list of numbers from 10 to 50. Next, we used Python map function to assign that addition to each item in a numbers.

By default, the Python map function returns the map object as an output value. So, we have to convert that object to any of the Iterable. The last print statement converts it to list and prints the mapped values.

def addition(num):
    return num + num
 
numbers = [10, 20, 30, 40, 50]
 
map_result = map(addition, numbers)
print(list(map_result))
[20, 40, 60, 80, 100]

TIP: Please refer Python List article to understand the lists.

Python map String Example

We are using both the numeric values and the list of characters. Here, the map function (addition, chars) means, for each character in the chars, it calls the addition. + symbol on characters or strings concat them.

def addition(num):
    return num + num
 
chars = ['a', 'b', 'c', 'd', 'e']
numbers = [10, 20, 30, 40, 50]
 
map_result = map(addition, chars)
print('Chars List = ', list(map_result))
 
result = map(addition, numbers)
print('Numbers List = ', list(result))
Chars List =  ['aa', 'bb', 'cc', 'dd', 'ee']
Numbers List =  [20, 40, 60, 80, 100]

Until now, we are using this on one iterable (single). In this example, we created a Python map function that accepts two arguments and returns the sum of those values. Next, we declared two numeric ones.

Here, (addition, numbers1, numbers2) means it takes first value from two lists and apply addition and so on. For example, (addition, 10, 150) becomes 160. Next, we are using the same scenario on fruits (string). It performs concatenation.

def addition(a, b):
    return a + b
 
numbers1 = [10, 20, 30, 40, 50]
numbers2 = [150, 250, 350, 450, 550]
 
map_result = map(addition, numbers1, numbers2)
print(list(map_result))
 
fruits1 = ['apple', 'orange', 'kiwi']
fruits2 = ['banana', 'cherry', 'berry']
 
result = map(addition, fruits1, fruits2)
print(list(result))
[160, 270, 380, 490, 600]
['applebanana', 'orangecherry', 'kiwiberry']

Python map built-in functions Example

Here, we are using the built-in function as the arguments. For this Python demo, we used factorial function, and len functions.

Here, the factorial func finds the factorial of each element of numbers. Next, the len finds the length of each item or fruit in the fruits.

import math
 
def factorial_func(num):
    return math.factorial(num)
 
def len_func(x):
    return len(x)
 
numbers = [1, 2, 3, 4, 5, 6, 7]
 
map_result = map(factorial_func, numbers)
print(list(map_result))
 
fruits = ['apple', 'orange', 'kiwi', 'banana', 'pineapple']

result = map(len_func, fruits)
print(list(result))
[1, 2, 6, 24, 120, 720, 5040]
[5, 6, 4, 6, 9]

Python map lambda Example

You can use the lambda expression inside this map function to make the code easily readable.

(lambda x: x * x, numbers) returns the square of each item in numbers. (lambda a:a.upper(), fruits) uses upper and converts each fruit to uppercase. Next, (lambda a:a.capitalize(), fruits) uses capitalize to capitalise the first character in each fruit.

numbers = [10, 20, 30, 40, 50, 60]
 
map_result = map(lambda x: x * x, numbers)
print(list(map_result))
 
fruits = ['apple', 'orange', 'kiwi', 'banana', 'pineapple']
 
result = map(lambda a:a.upper(), fruits)
print(list(result))
 
result2 = map(lambda a:a.capitalize(), fruits)
print(list(result2))
Python Map Function 5

Python map lambda Multiple arguments Example

Let me use multiple iterables in Python map function along with lambda. This example accepts two lists and adds each item in one with the another.

numbers1 = [10, 20, 30, 40, 50]
numbers2 = [150, 250, 350, 450, 550]
 
map_result = map(lambda x, y: x + y, numbers1, numbers2)
print(list(map_result))
 
fruits1 = ['apple', 'orange', 'kiwi']
fruits2 = ['banana', 'cherry', 'berry']
 
result = map(lambda a, b: a + b, fruits1, fruits2)
print(list(result))
[160, 270, 380, 490, 600]
['applebanana', 'orangecherry', 'kiwiberry']

Python map lambda Multiple functions Example

Until now, we show how to use a single Python map function with a list of values. However, you can also apply multiple funcs on each item. Here, we declared or defined three funcs. Next, we created a list of these three names.

First, we are using for loop with range to iterate values from 1 to 9. Next, we used it with lambda to assign each for loop item to list. It means for 1, it will call square_func, tripple_func and four_func similarly for 2 and so on.

def square_func(num):
    return num**2
 
def tripple_func(num):
    return num * num * num
 
def four_func(num):
    return num**4
 
function_list = [square_func, tripple_func, four_func]
 
for i in range(1, 10):
    number = map(lambda x: x(i), function_list)
    print("For ", i, " = " , list(number))
For  1  =  [1, 1, 1]
For  2  =  [4, 8, 16]
For  3  =  [9, 27, 81]
For  4  =  [16, 64, 256]
For  5  =  [25, 125, 625]
For  6  =  [36, 216, 1296]
For  7  =  [49, 343, 2401]
For  8  =  [64, 512, 4096]
For  9  =  [81, 729, 6561]

Convert map to list, set and tuple

Until now, we are showing the result as a list. However, you can convert this object output to any iterable such as tuple or set.

  • For list, use list(result).
  • For tuple, use tuple(result).
  • Same for the set. Use set(result).

First, we created a square_func to find the square of a given number and then declared a numbers. Next, we are converting the Python map function result to list, tuple, and set.

def square_func(num):
    return num**2
 
numbers = [10, 20, 30, 40, 50]
 
list_result = map(square_func, numbers)
my_list = list(list_result)
print(my_list)
 
tuple_result = map(square_func, numbers)
my_tuple = tuple(tuple_result)
print(my_tuple)
 
set_result = map(square_func, numbers)
my_set = set(set_result)
print(my_set)
[100, 400, 900, 1600, 2500]
(100, 400, 900, 1600, 2500)
{1600, 900, 2500, 100, 400 

You can use this to convert the result to a nested list or nested tuple. Here, we are calling the list along with the fruits.

fruits = ['apple', 'berry', 'kiwi']
 
result = map(list, fruits)
print(list(result))
 
result2 = map(tuple, fruits)
print(tuple(result2))
[['a', 'p', 'p', 'l', 'e'], ['b', 'e', 'r', 'r', 'y'], ['k', 'i', 'w', 'i']]
(('a', 'p', 'p', 'l', 'e'), ('b', 'e', 'r', 'r', 'y'), ('k', 'i', 'w', 'i'))