AppDividend
Latest Code Tutorials

Python Map Example | Python map() Function

0

Python map function is used to apply the function on all the elements of specified iterable and return map objects. 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

Content Overview

Python borrows the concept of the map from the functional programming domain.

Map operation takes the mapping function and a vector of data as arguments and returns the new vector, which is the outcome of applying the mapping function on each item of the vector independently.

It is a higher-order function taking a function and the variable number of iterables as arguments.

The function supplied to the map will thereby be applied columnwise to the iterables.

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.

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.

#How map() function works in Python

Okay, let’s take the following example.

#app.py

def calculateCube(n):
  return n*n*n

numbers = (11, 21, 19, 46)
output = map(calculateCube, numbers)
print(output)

# converting map object to set
numbersCube = set(output)
print(numbersCube)

See the output.

➜  pyt python3 app.py
<map object at 0x102b5e4e0>
{97336, 1331, 9261, 6859}
➜  pyt

In the above example, each item of the tuple is cubed.

Since map() expects the function to be passed in, lambda functions are commonly used while working with map() functions.

See the second 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 the 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

#Python map() Function with Python Lambda Function

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 the 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

#How to convert Python map to 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 a 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() function 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 multiple arguments

#How to listify the list of strings individually in Python

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

#app.py

listA = ['Millie', 'Finn', 'Noah', 'Gaten'] 
print("The original list is : " +  str(listA))
 
listifyData = list(map(list, listA)) 
print(listifyData)

See the output.

➜  pyt python3 app.py
The original list is : ['Millie', 'Finn', 'Noah', 'Gaten']
[['M', 'i', 'l', 'l', 'i', 'e'], ['F', 'i', 'n', 'n'], ['N', 'o', 'a', 'h'], ['G', 'a', 't', 'e', 'n']]
➜  pyt

Finally, Python Map Example | Python map() Function is over.

Recommended Posts

Python add to list

Python iter() example

Python string example

Python filter example

Python pandas example

Leave A Reply

Your email address will not be published.

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