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)
print(list(res))
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)
print(list(res))
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.
Note: Only a member of this blog may post a comment.