map() Function in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

The map() function in Python is a built-in function, where we can pass a function and any iterable (such as list, tuple, set, etc.), and it applies the given function to that iterable. After applying the function to all the elements, map returns a map object.

Syntax of map() Function in Python

Below given is the syntax for the map function in Python:

Parameters of map() Function in Python

In the image below, we have shown the parameters the map function in Python takes.

  • The first argument to map() is a transformation function. The transformation function modifies each original item in the list, into a new (transformed) item.
  • The transformation function can include built-in functions, classes, methods, lambda functions, and user-defined functions.
  • The second, and later parameters are the collections of iterables over which we want our function to be applied.
  • We can pass one or more iterables to our map() in python. The iterables can include list, tuple, set, etc. in python

Return Values of map() Function in Python

Map function in Python returns a map object after applying the given function to each item of the given iterable (list, tuple etc.).

We can then, pass over the map object to a list or tuple or set. Finally, we can fetch our result in the applied form.

How to use map() Function in Python?

To use the map function in Python, we should pass a transformation function and an iterable(or iterables) to our map function. The transformation function is applied to each of the items in the iterable and map object is returned.

Examples of map() Function in Python

Code 1:

Output:

Explanation:

  • Here we will calculate the square roots of the numbers using map(). Now, we define our user-defined function def square_root(n): to calculate the square root.
  • Then we pass over that function, and our numbers list to our map() function. map() applies sqrt() to every value in numbers.
  • We store our answer in result and print it. As output, we get our map object.

But, do we really want our result in this format? It is not even readable and understandable. So, to get our result into a readable format, we convert our map object into a list or tuple or a set, etc.

Output:

So, we convert our result into a list and finally, we get our converted result as the list. So, we should convert the result our map function returns(map object), into any iterable(like list, tuple, etc.)

Python map() with string

Code 1:

Output:

Explanation:

In the above example, we have used map passing to it a string. We transform the string into lowercase using our user-defined function to_lowercase .

Python map() with list

Code 2:

Output:

Explanation:

In the above example, we are converting a list of decimal numbers into binary. We are using our map function, and passing our transformation function along with the list.

Python map() with tuple

Code:

Output:

Explanation:

In the above example, we are converting a tuple of binary numbers into decimals. We are using our map function, and passing our transformation function along with the tuple.

Python map() with lambda

Code:

Output:

Explanation:

In the above example, we are using the lambda function as our transformation function. We are passing a list and our lambda function lambda x : x*x squares each of the numbers in the list.

if Statement with map()

Output:

Explanation

In this example, we use the map() function along with a custom function process_number() to apply a specific operation to each element of the input list. The process_number() function checks if the number is even or odd using an if statement, and then performs the appropriate operation (doubling for even numbers, squaring for odd numbers). The resulting list contains the processed values based on the condition, demonstrating the application of an if statement with map() without using lambda.

Python map() multiple arguments

Code:

Output:

Explanation:

In the above example, we pass 2 iterables(lists) to our map function. And, we use a lambda function to compute the product of elements from each of the iterable(list). Hence, for obvious reasons, we can use multiple iterables with map().

What if the iterables are of different sizes? In that case, the map function is applied to the elements until one of them is finished.

Code:

Output:

Converting map to list, tuple, set

Code:

Output:

Explanation:

In the above example, we have converted the map object we received after transforming our list, into the list, set, and tuple respectively.

Conclusion

  • map function in python applies a given function(transformer) to each item of an iterable (list, tuple, etc.) and returns an iterator.
  • map function in Python uses a technique commonly known as mapping, to perform the above task.
  • We also saw the syntax of map() and how can we use it with different kinds of functions like lambda function, normal function, and with different iterators like list, tuple, set, etc.

Explore Scaler Topics Python Tutorial and enhance your Python skills with Reading Tracks and Challenges.

See Also