AppDividend
Latest Code Tutorials

Python Map Function Tutorial With Example | Python Standard Library

0

Python Map Function Tutorial With Example | Python Standard Library is today’s topic. Python map() function is used to apply the function on all the elements of specified iterable and return map object. Python map() function applies the given function to each item of an iterable like the list, tuple, etc. and returns output as a list. Python map object is an iterator so that we can iterate over its elements.

Python Map Function Tutorial

The syntax of the map() is following.

map(function, iterable, ...)

function – The map() method passes each item of the iterable to this function.

iterable –  An iterable which is to be mapped like list, tuple, or set.

Python map() function applies a given to function to each item of an iterable and returns a list of the results.

The returned value from map() (map object) then can be passed to functions like list() (to create a list), set() (to create a set) and so on.

See the following example.

# app.py

chars = ['s', 'k', 'k', 'a', 'v']
def change_upper_case(s):
    return str(s).upper()

map_iterator = map(change_upper_case, chars)
output_list = list(map_iterator)
print(output_list)

In the above code, we have defined one list and defined a function that returns the uppercase string.

Then call a map function and provide the two arguments. 1) change_upper_case and 2) chars

The output from the map function is an iterator, and then we have converted that iterator to list and print that list.

So, the map function has mapped all lowercase chars to uppercase chars. Unlike filter function, it does not remove any value from the existing list.

See the below output.

 

Python Map Function Tutorial With Example | Python Standard Library

Use Map Function with Lambda Function in Python

We can use the Python map() function with Python Lambda function. We can reduce the above code to the three lines of code with the use of Lambda function.

We can use the lambda functions with a map() method if we don’t want to reuse it. It is useful when our function is small, and we don’t want to define a new function.

Let’s see the following example.

# app.py

chars = ['s', 'k', 'k', 'a', 'v']
converted = list(map(lambda s: str(s).upper(), chars))
print(converted)

 

Python Map Function Tutorial With Example

Converting map to the tuple and set

We can convert the map function iterator to the tuple and set. See the following example.

# app.py

names = ['krunal', 'ankit', 'rushabh', 'dhaval', 'nehal']
convertedTuple = tuple(map(lambda s: str(s).upper(), names))
print(convertedTuple)

See the below output.

 

Converting map to the tuple and set

We have converted the list to the tuple. Let’s convert the list to the set using map and lambda function.

# app.py

strings = ['krunal', 'ankit', 'rushabh', 'dhaval', 'nehal']
convertedSet = set(map(lambda s: str(s).upper(), strings))
print(convertedSet)

See the below output.

 

Python map() with lambda

Python map() function with multiple arguments

Take a look at the example of using map() function with multiple iterable arguments.

# app.py

list_numbers = [19, 18, 29, 46]
tuple_numbers = (21, 21, 21, 21)
map_iterator = map(lambda x, y: x * y, list_numbers, tuple_numbers)
print(list(map_iterator))

We have defined one list and iterator and passed that to the map function, and it will return the multiplication of that list and tuple and returns the iterator, and we have converted that iterator to the list.

See the output.

 

Python map() multiple arguments

Finally, Python Map Function Tutorial With Example | Python Standard Library is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.