Python Performance Showdown: Map() vs. For Loop

Denis Khodishchenko
Level Up Coding
Published in
4 min readMar 4, 2023

--

Which One Is Faster and When to Use Each Approach for Optimal Performance

Photo by Luca Tescari on Unsplash

Map function

The map() function in Python is a built-in function that takes in two arguments - a function and an iterable (e.g., a list or a tuple) - and returns a new iterable object that contains the results of applying the specified function to each element of the original iterable.

The general syntax of map() is:

map(function, iterable)

Here, function is the function that you want to apply to each element of the iterable, and iterable is the iterable object that contains the elements you want to apply the function to.

For example, let’s say you have a list of numbers and you want to square each of them. You can use map() to create a new list of squared numbers like this:

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

squared_numbers = map(lambda x: x**2, numbers)

print(list(squared_numbers)) # Output: [1, 4, 9, 16, 25]

In this example, lambda x: x**2 is the function that squares each element of the original list numbers. The map() function applies this function to each element of numbers, creating a new iterable object squared_numbers that contains the squared values. The list() function is used to convert the iterable object to a list so that it can be printed.

Note that map() returns an iterator, which means that it only generates values on the fly as they are needed. If you want to create a list or other iterable object containing the results of map(), you need to wrap it in a constructor like list(), as shown in the example above.

How does work under the hood

When map() is called, it creates a new iterator object that will iterate over the input iterable. It then applies the specified function to each element of the iterable using the __next__() method of the iterator.

But isn’t for loop using the same iterator protocol?

You are correct that both the map() function and a for loop use the iterator protocol with the __next__() method to iterate over an iterable. However, the performance difference between map() and for loop in Python can be attributed to a few factors.

  1. Lower-level implementation: As I mentioned earlier, map() is implemented in C and optimized for performance, whereas the for loop is implemented in Python and interpreted by the Python interpreter. Because C is a lower-level language than Python, operations performed in C are generally faster than equivalent operations in Python.
  2. Lazy evaluation: In Python 3, map() returns a lazy iterator object rather than a list object. This means that the function is only applied to each element of the iterable as it is requested, rather than all at once. This can be more memory-efficient, especially for large iterables where it may not be practical to store all the results in memory at once. In contrast, the for loop applies the function to all elements of the iterable at once and stores the results in memory.
  3. Built-in optimization: The C implementation of map() includes built-in optimizations that can make it faster than a Python for loop. For example, map() may be able to take advantage of vectorization and parallelization techniques to speed up the processing of large iterables.

Tests

Here’s an example to illustrate the performance difference between map() and for loop. Suppose we have a list of 1 million integers, and we want to apply a simple function that squares each integer:

import time

# Define the function to apply
def square(x):
return x ** 2

# Define the input iterable
input_list = list(range(1000000))

# Using map()
start_time = time.time()
output1 = list(map(square, input_list))
print(f"Map time: {time.time() - start_time}")

# Using a for loop
start_time = time.time()
output2 = []
for i in input_list:
output2.append(square(i))
print(f"For loop time: {time.time() - start_time}")

When I run this code on my machine, I get the following output:

Map time: 0.11022043228149414
For loop time: 0.18071722984313965

As you can see, the map() function is faster than the for loop in this case. However, the performance difference may vary depending on the specific operation being performed and the size of the iterable. It's always a good idea to test the performance of different approaches on your specific use case to determine which one is faster.

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Join the Level Up talent collective and find an amazing job

--

--

Python engineer and tech enthusiast sharing insights on IT careers, Python, tech, and finance. To work together: d.khodishchenko@gmail.com