How to use Python map()

The built-in Python function “map” allows you to use a function on all elements of an iterable.

What is Python map?

The Python map function is an elegant way to process the contents of iterables. Iterables are Python objects that can be iterated. Both Python lists and Python tuples can be iterables. The map function can be used instead of a Python for loop ,which takes every element in an iterable into account. Using map is helpful when you want to use the same function on an element. You can simply enter the function as a parameter.

What is the syntax for Python map?

Using Python map isn’t difficult. We have outlined the function’s syntax below:

map(function, iterable)
python

As you can see, the function uses two parameters. The first parameter describes the function that is to be used on every element of the iterable , and the second parameter is the iterable that you want to iterate. The function will return a Python map object with the function you passed applied to it. To work with the data, you can give the return value to functions such as Python list () or set().

How does Python map work?

To better understand how Python map works, let’s take a look at an example.

def increment(n):
	return n + 1
count = (0, 1, 2, 3, 4)
result = map(increment, count)
result_list = list(result)
python

First we’ve created a section of code with the function “increment”, which takes a number and increments it. Then the Python list called “count”, which contains the numbers 0 to 4, is created. Now, it’s time to call map. The first parameter is the function “increment”, which is defined in the code, and the second parameter is the list “count”. The result is then saved as the variable “result”.

So that you can continue working with the result, you can change the last line of the code to a list. If you view this list, for example, by using Python print ,you will see that all the elements from the list “count” were incremented. On your screen, you should see the following: “(1, 2, 3, 4, 5)”.

Tip

Python is the perfect choice if you’re working on a web project. With IONOS Deploy Now, you can easily deploy your web project via GitHub and build things automatically.

How to use Python map with lambda functions

If the functions you are passing to Python map are functions that you want to define for the elements of your iterable, it may make sense to pass the functions to the map function as a lambda expression. A Python lambda expression is essentially a short way of writing functions. You can use it if you want to use the function as a parameter. Since the functions being defined don’t have their own name and can’t be executed anywhere else in the code, we call them anonymous functions.

If you take another look at the code example from the previous section, you can replace the increment function with a suitable lambda expression and shorten the code:

count = (0, 1, 2, 3, 4)
result = map(lambda n: n + 1, count)
result_list = list(result)
python

This doesn’t actually change how the code works. We’ve only replaced the increment function with a lambda expression. This is set up by using the key word “Lambda”. We then enter a parameter, which would otherwise be written in the function head. After the colon, you can specify the return of the lambda expression. As you can see, the lambda expression makes the code much shorter.

How to pass multiple iterables to Python map

If you want to process two iterables, you can also use the Python map function. Let’s look at another example to see how we can use multiple iterables:

a = (2, 3, 4)
b = (3, 4, 5)
result = map(lambda x, y: x + y, a, b)
result_list = list(result)
python

In the example above, we created two lists named “a” and “b”, each with three numbers. The map function is called using an anonymous function with a lambda expression as an argument. This time, the lambda function takes the two parameters, x and y, and returns the sum of the two elements. As an additional parameter, we pass both lists to Python map and convert the result of the function call into a list. The result is “(5, 7, 9)”, the sum of the different numbers in both lists.

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.