In this tutorial, you’ll learn how to use the Python map() function to apply a function on all items of an iterable.
Python supports the functional programming paradigm that allows you to define tasks as a computation of functions programmatically. You can treat Python functions as objects: a function can take another function as a parameter and return another function.
The map() function takes in a function as an argument and lets you apply it to all items in a sequence.
By the end of this tutorial, you’ll be able to use the Python map() function — to rewrite verbose loops and list comprehensions. You’ll code several examples to understand the different ways you can use the map() function.
How to Apply a Function to Elements of a Python List
Let’s start our discussion with an example.👩🏫
Here nums is a list of numbers.
nums = [2,4,3,7]
Next, consider the function self_pow(). The function self_pow() takes in a number as the argument and returns the number raised to the power of the number itself: n**n.
In Python, ** is the exponentiation operator. a**b returns the value of a raised to the power b, ab.
TO-DO: To create a new list nums_pow by applying the function self_pow() to every element in list nums.
For every number in the nums list, call the function self_pow() with num as the argument.
Append the result of the function call to the new list nums_pow.
nums_pow = 
for num in nums:
In the output, every number nums is raised to itself. The elements in nums_pow list are as follows: 22, 44, 33,77.
[4, 256, 27, 823543]
Using List Comprehension
You can make this concise using list comprehension. From the explicit for loop above, we can identify the output expression, and the list to loop through.
We can then modify the generic list comprehension expression:
new_list = [<output expression> for item in iterable]
The list comprehension expression to generate the nums_pow list is as follows:
nums_pow = [self_pow(num) for num in nums]
The output is the same as that from using for loops, as expected.
[4, 256, 27, 823543]
Instead of loop and list comprehension, you can use the Python map() function with a concise syntax that helps apply the function to all items in an iterable. Let’s start by learning the syntax of the map function.
Python map() Function Syntax
The general syntax to use thePython map() function is as follows:
The list strings_upper includes strings in the list strings formatted in uppercase.
#2. The built-in len() function in Python takes in a sequence as the argument and returns its length. To find the length of each of the strings in the strings list, we can use the map() function and apply the length function on each string, as shown below.
The outputs are identical in both of the above approaches. However, you should ensure that the code is readable and maintainable when nesting functions as shown above.
How to Use map() Function with Lambda Functions
In the previous sections, you learned how to use the Python map() function with built-in and user-defined functions. You’ll now learn how to use the map() function with lambda functions, which are anonymous in Python.
Sometimes, you’ll have a function whose body contains only one line of code, and you may need to use the function only once and not reference it elsewhere in the program. You can define such functions as lambda function in Python.
Note: This is a generalization of string slicing expression str[start:stop:step].
– Without the start and stop values, the slice starts at the beginning of the string and extends up to the end of the string. – Negative values of step gives slices starting from the end of the string. – Therefore, str[::-1] returns a reversed copy of str.
You can use this lambda function: lambda x:x[::-1]Inside the map function, as shown below.
Bala Priya is a developer and technical writer from India with over three years of experience in the technical content writing space. She shares her learning with the developer community by authoring tech tutorials, how-to guides, and more…. read more
Python is a very versatile language, and Python developers often have to work with a variety of files and get information stored in them for processing. One popular file format you’re bound to encounter as a Python developer is the Portable Document Format popularly known as PDF