Python Map() Method

Python map method; Through this tutorial, i am going to show you what is map method and how to use map() method with list, tuple, set, dictionary in python.

Python map function

map() function in python, which is used to apply a function on all the elements of specified iterable and return map object. Python map object is an iterator, so we can iterate over its elements. We can also convert map object to sequence objects such as list, tuple etc.

The syntax of the map() is the 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, dictionary, or set.

How map() function works in Python

You have already understood above that what is the map () function of Python, how to write its syntax and how many arguments can be passed to it etc.

Now let us know how the map () function of Python works. Let’s understand by examples:

def calculateCube(n):
  return n*n*n
numbers = (2, 5, 10, 12)
output = map(calculateCube, numbers)
# converting map object to set
numbersCube = set(output)

See the following output of above python map() function program:

[8, 125, 1000, 1728]
set([8, 1000, 1728, 125])

In the above python map() function program, each item of the tuple is cubed.

Let’s take another example to understand Python’s map () function:

chars = ['p', 'y', 't', 'h', 'o', 'n']
def change_upper_case(s):
    return str(s).upper()
map_iterator = map(change_upper_case, chars)
output_list = list(map_iterator)

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

After we call a map function inside python program with the following arguments change_upper_case and chars.

In Python, the map function will convert one character to uppercase and print that list.

In this Python program, the map function will map all lowercase variables to uppercase. Unlike the filter function, it does not remove any value from the existing list.

You can see the output of the above python program:

['P', 'Y', 'T', 'H', 'O', 'N']

Python map() Function with Python Lambda Function

Now you will learn how to use the Python map() function with Python Lambda function.

If you use the lambda function with the map () function in Python. So by reducing the code of the given Python program, you can write it in only 3 lines.

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 take an example of map() function with python lambda function:

chars = ['p', 'y', 't', 'h', 'o', 'n']
converted = list(map(lambda s: str(s).upper(), chars))

The output of the above program:

['P', 'Y', 'T', 'H', 'O', 'N']

How to convert Python map to tuple and set

Do you know that you can convert to the tuple and set by using Python’s map function iterator?

Let’s take the example for better understanding:

chars = ['p', 'y', 't', 'h', 'o', 'n']
convertedTuple = tuple(map(lambda s: str(s).upper(), chars))

See the following output of the above program:

('P', 'Y', 'T', 'H', 'O', 'N')

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

chars = ['p', 'y', 't', 'h', 'o', 'n']
convertedSet = set(map(lambda s: str(s).upper(), chars ))

See the following output of the above program:

set(['H', 'O', 'N', 'P', 'T', 'Y'])

Python map() function with multiple arguments

Next thing, let us now understand how to use the map () function in Python with multiple iterable arguments:

list_numbers = [1, 5, 8, 9]
tuple_numbers = (11, 20, 54, 23)
map_iterator = map(lambda x, y: x * y, list_numbers, tuple_numbers)

First of all, in the above program 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 of above python map() function with multiple argument example:

[11, 100, 432, 207]

How to listify the list of strings individually in Python

Let’s listify the list of strings individually in Python.

listA = ['python', 'java', 'php', 'core'] 
print("The original list is : " +  str(listA))
listifyData = list(map(list, listA)) 

See the output of python listify the list program:

The original list is : ['python', 'java', 'php', 'core']
[['p', 'y', 't', 'h', 'o', 'n'], ['j', 'a', 'v', 'a'], ['p', 'h', 'p'], ['c', 'o', 'r', 'e']]

Recommended Python Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.