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 function returns a list of the result values. In this section, we discuss how to use this along with example and the basic syntax of the Python map function is

map(function_name, iterables,...)

Python map Values Example

In this 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 it to assign that addition to each item in a numbers.

By default, this Python 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 values.

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

TIP: Please refer List article to understand them.

Python map String Example

We are using both the numeric values and the characters. Here, the (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]
 
res = map(addition, chars)
print('Chars List = ', list(res))
 
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]
 
res = map(addition, numbers1, numbers2)
print(list(res))
 
fruits1 = ['apple', 'orange', 'kiwi']
fruits2 = ['banana', 'cherry', 'berry']
 
result = map(addition, fruits1, fruits2)
print(list(result))
[160, 270, 380, 490, 600]
['applebanana', 'orangecherry', 'kiwiberry']

Built-in functions Example

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

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]
 
result = map(factorial_func, numbers)
print(list(result))
 
fruits = ['apple', 'orange', 'kiwi', 'banana', 'pineapple']

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

Python map lambda Example

You can use the lambda expression inside this 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.

Python map Function 5

lambda Multiple arguments Example

Let me use multiple iterables 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]
 
res = map(lambda x, y: x + y, numbers1, numbers2)
print(list(res))
 
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 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 one with 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]

Python map to convert 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.

  • 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]
 
lt_result = map(square_func, numbers)
my_list = list(lt_result)
print(my_list)
 
tp_result = map(square_func, numbers)
my_tuple = tuple(tp_result)
print(my_tuple)
 
st_result = map(square_func, numbers)
my_set = set(st_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 iterable along with the fruits.

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