Jump to content

Python Map() Function Tutorial


Recommended Posts

In Python, sometimes we need to perform the operation on every element of the iterable to create a new iterable. To do this, the “for” loop is normally used in Python. But we can also perform this using the “map()” function. The “map()” function is used for mapping the iterable value based on the specified function. It creates a new iterator by executing the transformation function for each item/element.

This tutorial follows these outlines:

What is the Python “map()” Function?

In Python, the “map()” function executes the particular function for each element in an iterable or sequence. We can pass the item to the “map()” function as a parameter.

Syntax

map(function, iterable)

Parameters

In this syntax:

  • function: This specifies the function to execute for each element/item.
  • iterable: This specifies the collection, sequence, or iterator object.

Return Value

The “map()” function in Python retrieves the mapping object of the class map. We can use the “list()” or “set()” function to convert the mapping object into a list or sets of values.

Example 1: Python “map()” Function Working

In this example, we first declare the custom function. This function retrieved the square of the given parameter value. Next, the “map()” function applies the custom function to each iterator value of the tuple and retrieved map object. Lastly, the map object is converted into a set sequence using the “set()” function:

def func(x):
    return x*x

num = (5, 3, 1, 2)
res = map(func, num)
print(res, '\n')
print(set(res))

The map object and sequence value have been returned successfully:

image1-51.png

Example 2: Applying Function on Two Iterable Objects

Here, we apply the custom function on two iterable objects. First, we declared the custom function that accepts two parameters and retrieves the sum. Next, the “map()” function takes the function and two iterables as an argument to apply the function on two iterables. Lastly, the map object is converted into a list:

def func(x, y):
    return x+y

num = [5, 3, 1, 2]
num1 = [9, 6, 5, 2]
res = map(func, num, num1)
print(res, '\n')
print(list(res))

The list value has been retrieved:

image3-46.png

Example 3: Applying Standard Function on Iterable

Here, the map() function applies the inbuilt “len()” function on a given iterable and retrieves the map object. We used the “list()” function to convert the map object into a list that contains the length of all the string elements:

list1 = ['Joseph', 'Lily', 'Anna', 'Jon']
res = map(len, list1)
print(res, '\n')
print(list(res))

This code retrieves/shows the below output:

image2-49.png

Example 4: Applying Lambda Function on Iterable

We can utilize the lambda function instead of separately declaring custom functions in the program. The lambda function is a one-line anonymous function. Here, we use the map() function to accept the lambda function and apply it to the given iterable sequence:

num = (5, 10, 15, 20)
res = map(lambda x: x + x, num)
print(list(res))

The addition of the number has been derived:

image5-35.png

Example 5: Python “map()” Function With “if-else” Statement

We can also customize our function with different condition statements, such as the “if-else” statement. Here, the custom function will multiply the iterable element with “2” if the element is even, otherwise the same element is printed. Lastly, the map() applies the function to iterable and converts it into a list using list():

def even(x):
    if x % 2 == 0:
        return x * 2
    else:
        return x

res = map(even, [10, 23, 34, 45, 55])
print(list(res))

This code will show this:

image4-40.png

Conclusion

The “map()” function is utilized in Python to retrieve the mapping object by applying the particular function to a given iterable sequence. We can convert the mapped object into various sequences such as lists, sets, or tuples. This tutorial provided multiple examples of Python map() functions.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...