The Python map() function applies a function on all items in an input iterable object. The iterable object can be a list, a tuple, a set, dictionary or a string. The map() function returns an iterable map object, which you will have to iterate over using a for loop or using the list() or set() methods to get the final output. The syntax of the map() function is:

map(function, iter)

Parameters:

function: Compulsory function which the map function applies on the elements of the given iterable
iter: Compulsory iterable object. You can pass multiple iterable objects to the map() function.

This tutorial will detail the map() function and look at examples with different functions, including lambda.


How Does the map() Function Work?

The map() function takes a function and an iterable object as input. The map() function applies the passed function to all elements in the given iterable object. Let’s look at an example where we have a list of numbers, and we want to determine the square root of each of the numbers in the list. We can define a function to calculate the square root of a number using the exponentiation operator and pass this and the list of numbers to the map function. Let’s look at the code: ≻

# Define function

def square_root(n):
    return n ** 0.5

# Declare list variable to store integer values

list_of_numbers = [4, 9, 16, 25, 36, 49]

# Use map() to get square root of numbers

square_root_list = map(square_root, list_of_numbers)

# Print map object 

print(square_root_list)

# Print final output

print(list(square_root_list))

Let’s run the code to see the result:

<meta charset="utf-8">≺map object at 0x7f7b69e64c40<meta charset="utf-8">≻
[2.0, 3.0, 4.0, 5.0, 6.0, 7.0]

The output of the map() function is a map object. To get the final output, we need to iterate over the map object using a for-loop or list() or set(). We use the list() method in the above code to get the final output. The square_root_list variable stores the square roots of the numbers in the list_of_numbers variable.

Using the map() Function with Lambda Function

In Python, we can use lambda expressions to construct anonymous functions, and we can use the lambda keyword similar to using def to define normal functions. Let’s look at an example where we have a list of numbers, and we want to square each number using a lambda function.

# Define a list 

list_of_numbers = [3,4,5,6,7,8,9]

# Use map function to square numbers

squares_list = map(lambda x: x ** 2, list_of_numbers)

# Print map object

print(squares_list)

# Print final output using list

print(list(squares_list))

Let’s run the code to get the result:

<meta charset="utf-8">≺map object at 0x7f7b6992f100<meta charset="utf-8">≻
[9, 16, 25, 36, 49, 64, 81]

Using the map() Function with Multiple Iterables

The map() function can take more than one iterable object. Let’s look at an example where we have two lists of numbers, and we use a lambda function to perform element-wise addition of the two lists.

# Define two lists

numbers_1 = [2, 4, 6, 8]

numbers_2 = [1, 3, 5, 7]

# Apply map function

result = map(lambda x, y: x + y, numbers_1, numbers_2)

# Print map output

print(result)

# Print final output

print(list(result))

The map() function takes two lists in the above code and adds them together using the lambda function. Let’s run the code to get the result:

<meta charset="utf-8">≺map object at 0x7f7b69e645b0<meta charset="utf-8">≻
[3, 7, 11, 15]

Using the map() Function with Multiple Different Iterable Types

The map() function can take in multiple iterable objects of different types. Let’s look at an example where we want to perform element-wise division on a list by a tuple. We will define a function that divides one number by another and returns the calculation result. Then, we can pass the list and tuple of numbers to the map() function together with the division function.

lst = [2, 4, 5, 8, 10]

tup = (3, 6, 9, 1, 5)

def div_func(a, b):

    return a/b

result = map(div_func, lst, tup)

print(result)

print(list(result))

Let’s run the program to get the result:

≺map object at 0x7fb3f840fb80≻

[0.6666666666666666, 0.6666666666666666, 0.5555555555555556, 8.0, 2.0]

The program successfully divides the numbers in the list by the numbers in the tuple and prints the result to the console.

Using the map() Function on a String

A string is an iterable object and can serve as input to the map function. Let’s look at an example of using the map() function to convert a string to all uppercase. First, we will define an uppercase conversion function using the upper() function. Then we will define a string and pass it to the map() function and the uppercase function.

def upper_func(s):
    return s.upper()

def show_result(map_object):
    for item in map_object:
        print(item, end='')

my_str = "python is really fun to learn!"

uppercase_str = map(upper_func, my_str)

print(uppercase_str)

show_result(uppercase_str)

The code above defines a function to iterate over the map object using a for-loop and print the result. Let’s run the code to get the output:

<meta charset="utf-8">≺map object at 0x7f7b69e10e50<meta charset="utf-8">≻
PYTHON IS REALLY FUN TO LEARN!

Using the map() Function with Python Built-in Functions

list_of_floats = [3.134, 4.603, 2.012, 5.298, 7.043]

rounded_list = map(round, list_of_floats)

print(rounded_list)

print(list(rounded_list))
<meta charset="utf-8">≺map object at 0x7f7b69ee4160
[3, 5, 2, 5, 7]

Using the map() Function with Tuple

A tuple is an object in Python with items separated by commas and enclosed in round brackets. Let’s look at an example where we have a tuple of integers. We will define a function that will cube each number in the tuple and pass the function to the map() function. Let’s look at the code:

def cube_func(n):
    return n ** 3

my_tuple = (2, 3, 4, 5, 6)

cubed_numbers = map(cube_func, my_tuple)

print(cubed_numbers)

print(list(cubed_numbers))

In the above code, we pass the function called cube_func to the map() function and the tuple containing five integers. We then print the map object and the final output from the map object using the list function. Let’s run the code to get the result:

<meta charset="utf-8">≺map object at 0x7f7b69ee8d90<meta charset="utf-8">≻
[8, 27, 64, 125, 216]

Summary

Congratulations on reading to the end of this tutorial! You have gone through how to use the map() function with different iterable objects and different types of functions. The map() function helps reduce the amount of code you need to write whilst making your code more readable and less prone to bugs.

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!