Tuesday, April 21, 2020


Python map() function

Map() function :-

map() function in python used to perform operations on a given number of iterable sequence like list, set, tuple etc.

map() function takes a function as an one argument and a iterable sequence as a second argument. 

map(func, iter)

func:- This is a function on which map function passes each element of given iterator.
iter:- This is a iterable sequence like lists, sets, tuples etc.

map() function returns a map object. so you have to change the type of result to list or tuple as per your requirement.

Example 1:- Python program for getting triple of a given numbers list using map().

# Return triple of n 
def triple(n): 
    return n**3 
# We make triple all numbers using map() 
numbers = (1, 2, 3, 4) 
res = map(triple, numbers) 

Output :- [1, 8, 27, 64]

Example 2:- get triple of a given numbers list using lambda and map() function.

# We make triple of all numbers using map() 
numbers = (5, 2, 6, 3) 
res = map(lambda x: x**3, numbers) 

Output :- [125, 8, 216, 27]

I tried my best to explain the one of special python function. So if you still have any difficulty, you can drop a comment here.  Thank you.

About Author -

Hi, I am Anil.

Welcome to my eponymous blog! I am passionate about web programming. Here you will find a huge information on web development, web design, PHP, Python, Digital Marketing and Latest technology.

Subscribe to this Blog via Email :