Python map() Function

The map() is a built-in function in Python that is used to apply a given function to each item of an iterable (e.g. list, tuple, etc.) and returns a map object (map object at 0x7f86d170c4c0) which is an iterator. You can use it with for loop if you wanted or convert it to a list using list().

The input function is called with each item of the iterable as an argument, and the returned map object contains the return values of the function applied to each item. In this article, I will explain the syntax of the Python map() function, its parameters and explain how to map() function applies a specified function to each item of an iterable.

2. Syntax of map() Function

Following is a syntax of the map() function.


# Syntax of map() function
map(function, iterable, [iterable1, iterable2, ...])

2.1 Parameters of map()

The map() function takes the following optional parameters.

  • function – A function is a callable object (i.e. a function, method, or lambda expression) that will be applied to each item of the iterable (e.g. list, tuple, etc.).
  • iterable – It is iterable which is the collection of items on which the function will be applied. You can use one or more iterables.

3. Python map() Function Example

You can use Python map() function to apply the transformation function on each item of the iterable. We used to do this traditionally with for loop, but by using map() function you can avoid using explicit for loop in your code. Using the map() function makes your code readable and user-friendly. Try to avoid the looping when possible and use the map() function instead.

Let’s create a simple square() function and use it with map() function by taking a tuple of numbers. Here, is an example.


# Create tuple of numbers
numbers = (2, 4, 6, 8, 5)
print("Original:", numbers)

# Create function square
def square(x):
    return x * x

# Use map() function to apply a function
square_numbers = map(square, numbers)
result = list(square_numbers)
print("Result:",result)

# Output:
# Original: (2, 4, 6, 8, 5)
# Result: [4, 16, 36, 64, 25]

Here, the square() function is executed for each element in the numbers tuple. The square() function takes a single argument x, and returns the value of x * x. When the map() function is called with square() function and numbers as arguments, it applies the square() function to each element in the numbers tuple and returns an iterator object. The list() function is then used to convert the iterator to a list.

4. Using map() with Set

A Set in Python is created using curly brackets {}, and it is also an iterator, so you can use it inside the map() function to apply a function to each item in the set. Since the Python set is unordered, you will get output in a different order.

In the below examples, I have created multiple() and square() functions and used them with the map() function.


# Using map() with set
def multiply(x):
    return x * 10

# Create numbers
numbers = {2, 4, 6, 8, 5}
multiply_numbers = map(multiply, numbers)
print(list(multiply_numbers))

# Output:
# [20, 40, 50, 60, 80]

# Using map() with set in python
def square(x):
    return x ** 2
numbers = {2, 4, 6, 8, 5}
squared_numbers = map(square, numbers)
print(list(squared_numbers))

# Output:
# [4, 16, 25, 36, 64]

5. Using map() with List of Numbers

Here, I will use two iterators as arguments to the Python map() function. First, create addtion(x,y) that takes two arguments x and y and return value by adding these two. Use this function on map() function along with two iterable as arguments.


# Create function that takes two arguments
def addition(x,y):
    return x + y

# Create lists
numbers1 = [2, 4, 6, 8, 5]
numbers2 = [3, 2, 1, 1, 4]
print("Numbers1: ",numbers1)
print("Numbers2: ",numbers2)

# Using map() with two iterables
addition_numbers = map(addition, numbers1, numbers2)
result = list(addition_numbers)
print("Result:",result)

# Output:
# Numbers1:  [2, 4, 6, 8, 5]
# Numbers2:  [3, 2, 1, 1, 4]
# Result: [5, 6, 7, 9, 9]

When the map() function is called with addition() function and numbers1 and numbers2 as arguments, it applies the addition() function to each element from the two arguments and returns an iterator object.

6. Use map() Function with Lambda

You can also use the lambda function as a function argument to the map(). The map() function along with a python lambda function performs an operation of addition on each element of the tuple numbers. The lambda function takes a single argument x and returns the result of x + x. The map() function applies the lambda function to each element of the numbers tuple, creating an iterator of the results. The list() function is then used to convert this iterator into a list


# Use map() function & lambda
numbers = (2, 4, 6, 8, 5)
addition_numbers = map(lambda x: x + x, numbers)
print(list(addition_numbers))

# Output:
# [4, 8, 12, 16, 10]

7. Use map() Function & Lambda with Multiple Arguments

Let’s use another example of map() function that takes multiple iterable arguments as inputs and applies the lambda function to the corresponding elements of each input iterable. In this case, the first element of input iterables is passed as the first argument to the lambda function, and the second element of input iterable is passed as the second argument. The lambda function here sums up the two arguments passed to it and returns the result, so the elements of the numbers1 and numbers2 are being added.


# Use map() function & lambda to multiple arguments
numbers1 = [2, 4, 6, 8, 5]
numbers2 = [1, 3, 5, 7, 4]
addition_numbers = map(lambda x, y: x + y, numbers1, numbers2)
print(list(addition_numbers))

# Output:
# [3, 7, 11, 15, 9]

8. Use map() to list of strings individually

Similarly, if you have a list of strings and you want to use the map() function to process each string individually, you can pass a function that operates on a string to the map() function, along with the list of strings.


# Use map() to list of strings individually
strings = ['Hadoop','Spark']
test = list(map(list, strings))
print(test)

# Output:
# [['H', 'a', 'd', 'o', 'o', 'p'], ['S', 'p', 'a', 'r', 'k']]

Conclusion

In this article, I have explained the python map() function syntax, parameters, and how to use it to apply the transformations on tuples, sets, and lists. And also explained how to use multiple iterable and finally use map with lambda functions.

For more examples of functions refer to Python Built-in Functions.

Happy Learning !!

References

Leave a Reply

You are currently viewing Python map() Function