Python’s built-in map function is used to apply a function to all elements of what is called an « iterable ».
Fields of application of Python map¶
The Python map function is a elegant way to process the contents of iterables. Iterables are Python objects that you can iterate through. In addition to Python lists, there are also, for example, Python tuples. The map function can be used in principle instead of a loop for Python, which examines each element of the iterable. The use of map is above all useful if you want apply the same function to each of these elements. You can then simply pass them to the function as a parameter.
Python map function syntax¶
The syntax of Python map is easily accessible:
map(function, iterable)
python
As you can see in this sample code, the function accepts two parameters. The first parameter describes the function to be applied to each element of your iterable, and the second parameter corresponds precisely to this iterable, over which you want to iterate. The function returns a object of style Python map, to which the function you passed was applied. You can pass this return value to functions like Python list() or set() to manipulate the data.
Python map in practice¶
We can rely on a code example to illustrate the behavior of the Python function:
def incrémenter(n):
return n + 1
nombres = (0, 1, 2, 3, 4)
résultat = map(incrémenter, nombres)
liste_résultat = list(résultat)
python
In this code snippet, a function is created named « increment », which accepts a number and performs precisely what its name indicates. It follows the creation of a Python list called « numbers », which contains the numbers from 0 to 4 inclusive. The map call is the exciting part: the function defined in the code as « increment » is passed as the first transfer parameter and the list « numbers » is passed as the second transfer parameter. The result is stored in the variable called “result”.
To allow you to continue working with the result, the result of the last line in the sample code is converted to a list. For example, if you view this list with Python print, you will find that all items in the « numbers » list passed to map have been incremented by 1. The following result would then appear on the screen: “(1, 2, 3, 4, 5)”.
Python is an ideal language for carrying out web projects. With Deploy Now from IONOS, you can deploy your web projects via GitHub and compile them automatically in a very easy way.
Python map with lambda functions¶
If the functions you pass to Python map are just functions you want to define for the elements of your iterable, it might be useful to pass the function to the map function as a lambda expression. A Python lambda expression is basically a shorthand for functions you can use, if you want to pass them as a pass parameter. Since the functions thus defined have no proper name and cannot be called elsewhere in the code, they are also called anonymous functions.
If we consider again the example code already explained to use Python map, we can also replace the « increment » function with an appropriate lambda expression and thus shorten the code:
nombres = (0, 1, 2, 3, 4)
résultat = map(lambda n: n + 1, nombres)
liste_résultat = list(résultat)
python
The behavior of the code does not change at all in principle. Only the « increment » function has been replaced by a lambda expression. It is introduced with the keyword “lambda”. It is followed by a parameter that you would otherwise write in the header of your function. You can specify the return of the lambda expression after the colon. As you can see, this variant of the code is much shorter thanks to the lambda expression.
Pass multiple iterables to Python map¶
If you want to process two iterables, you can also use the Python map function for this purpose. An example illustrates how to apply the map function to two iterables:
a = (2, 3, 4)
b = (3, 4, 5)
résultat = map(lambda x, y: x + y, a, b)
liste_résultat = list(résultat)
python
In the example above, two lists named « a » and « b » have been created, each containing three numbers. The map call again uses an anonymous function with a lambda expression. This now accepts two parameters x and y and returns the result of their addition. We pass two lists to Python map as an additional parameter and convert the result of the function call back to a list. The result is « (5, 7, 9) » because the numbers in the two lists have been added together in some places.