Python Map
Estimated reading: 2 minutes
896 views
Python Map introduction
Python map() function
map() function returns a list of the results after applying the given function to each item of a given iterable (list, tuple etc.)
Syntax :
map(fun, iter)
Returns :
Returns a list of the results after applying the given function
to each item of a given iterable (list, tuple etc.)
Returns :
Returns a list of the results after applying the given function
to each item of a given iterable (list, tuple etc.)
def addition(n):
return n + n
# We double all numbers using map()
numbers = (1, 2, 3, 4)
result = map(addition, numbers) # map(function,Structre)
print(result)
print(tuple(result))
#Example
#Calculate the length of each word in the tuple:
def myfunc(n):
return len(n)
x = map(myfunc, ('apple', 'banana', 'cherry'))
#convert the map into a list, for readability:
print(list(x))
#Example
#Calculate the length of each word in the tuple:
def myfunc(n):
return len(n)
x = map(myfunc, ('apple', 'banana', 'cherry'))
#convert the map into a list, for readability:
print(list(x))
# Add two lists using map and lambda
numbers1 = [1, 2, 3]
numbers2 = [4, 5, 6]
result = map(lambda x, y: x + y, numbers1, numbers2)
print(list(result))
# Research and give me the answer and Explain me also that
# List of strings
l = ['sat', 'bat', 'cat', 'mat']
# map() can listify the list of strings individually
test = list(map(list, l))
print(test)