map(function,iterable)
function can be built in or user definedmy_list=['Alex','Ronald','John']
my_list1=map(len,my_list) # using built in function len
print(list(my_list1))
Output
[4, 6, 4]
MATH=[20,30,40]
def my_function(n):
return n+5
my_list1=map(my_function,MATH)
print(list(my_list1))
Output
[25, 35, 45]
MATH=[20,30,40]
def my_function(n):
return n+5
my_list1=map(lambda n:my_function(n),MATH)
print(list(my_list1))
MATH=[20,30,40]
ENGLISH=[30,40,50]
SCIENCE=[40,50,60]
def my_function(a,b,c):
return a+b+c
my_list1=map(my_function,MATH,ENGLISH,SCIENCE)
print(list(my_list1))
Output
[90, 120, 150]
my_list=['You','have','to','pass','in',3,'languages']
my_str = ' #'.join(map(str,my_list)) # from list create string
print(my_str)
When the required output is a list, a list comprehension can also be used to transform each item of an iterable.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.