How to Use the map() Function in Python?

In this tutorial, I will explain how to use the Python map function in Python. In one of my projects with the USA clients, I encountered this scenario while working with large datasets and performing repetitive tasks. Let us see how the various map() methods in Python work, with examples.

map() Function in Python

The map function in Python takes two arguments: a function and an iterable. It applies the specified function to each item of the iterable and returns a map object (an iterator) of the results. The general syntax is:

map(function, iterable)

Read How to Use the randint() Function in Python?

Examples

Let us see some examples to understand more about the topic.

Example 1: Convert Temperatures from Celsius to Fahrenheit

Suppose you have a list of temperatures in Celsius and you want to convert them to Fahrenheit. Here’s how you can do it using the map function:

celsius_temperatures = [0, 20, 30, 40, 100]

def celsius_to_fahrenheit(celsius):
    return (celsius * 9/5) + 32

fahrenheit_temperatures = list(map(celsius_to_fahrenheit, celsius_temperatures))
print(fahrenheit_temperatures)

Output:

[32.0, 68.0, 86.0, 104.0, 212.0]

I have executed the above example code and added the screenshot below.

the map() Function in Python

Check out How to Use wait() Function in Python?

Example 2: Capitalize Names

Imagine you have a Python list of names and you want to capitalize each name. Using the map function, you can achieve this effortlessly:

names = ["john", "jane", "michael", "susan"]

capitalized_names = list(map(str.capitalize, names))
print(capitalized_names)

Output:

['John', 'Jane', 'Michael', 'Susan']

I have executed the above example code and added the screenshot below.

Use the map() Function in Python

Example 3: Calculate Square Roots

If you have a list of numbers and you want to calculate the square root of each number, you can use the map function with the math.sqrt function:

import math

numbers = [4, 16, 25, 36, 49]

square_roots = list(map(math.sqrt, numbers))
print(square_roots)

Output:

[2.0, 4.0, 5.0, 6.0, 7.0]

I have executed the above example code and added the screenshot below.

How to Use the map() Function in Python

Handle Multiple Iterables with map() Function in Python

The map function can also handle multiple iterables. When doing so, the function passed to map should accept as many arguments as there are iterables. Here’s an example:

Example 4: Add Corresponding Elements of Two Lists

Suppose you have two lists of numbers and you want to add the corresponding elements of each list:

list1 = [1, 2, 3, 4]
list2 = [10, 20, 30, 40]

def add(x, y):
    return x + y

result = list(map(add, list1, list2))
print(result)

Output:

[11, 22, 33, 44]

Read Is Python a Good Language to Learn?

Use Lambda Functions with map() in Python

Lambda functions are anonymous functions defined using the lambda keyword. They are often used with the map function for simple operations. Here’s an example:

Example 5: Double Numbers

If you want to double each number in a list, you can use a lambda function with map:

numbers = [1, 2, 3, 4, 5]

doubled_numbers = list(map(lambda x: x * 2, numbers))
print(doubled_numbers)

Example: Data Transformation for a US-Based Company

Let’s consider a real-world scenario where you’re working for a US-based e-commerce company. You have a list of product prices in different formats (strings, floats, etc.), and you need to convert all prices to floats for further processing. Here’s how you can use the map function to achieve this:

Example 6: Convert Product Prices to Floats

prices = ["19.99", 29.99, "15.49", "23.89", 49.99]

converted_prices = list(map(float, prices))
print(converted_prices)

Check out What Is the Best Way to Learn Python?

Combine map() Function with Other Functions

The map a function can be combined with other functions like filter and reduce to perform more complex operations.

Example 7: Filter() and Map() Function in Python

Suppose you have a list of ages and you want to filter out ages below 18 and then calculate the squares of the remaining ages:

ages = [15, 18, 21, 16, 30, 25]

filtered_ages = filter(lambda x: x >= 18, ages)
squared_ages = list(map(lambda x: x ** 2, filtered_ages))
print(squared_ages)

Check out How to Use the Python pop() Function?

Conclusion

In this tutorial, I helped you to learn how to use the Python map function in Python. I discussed what is map() function is in Python, we saw how to handle multiple iterables with the map() function in Python, using the lambda function in a map(), a real-time example, and how to combine the map() function with other functions.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.