xiaohuamao
0
Q:

map function in python

# 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)) 

Output:
[5, 7, 9]
15
'''try this instead of defining a function'''

a = [1,2,3,4]
a = list(map(lambda n: n+n, a))

print(a)
2
map(function_to_apply, list_of_inputs)
2
# Python program to demonstrate working 
# of map. 
  
# Return double of n 
def addition(n): 
    return n + n 
  
# We double all numbers using map() 
numbers = (1, 2, 3, 4) 
result = map(addition, numbers) 
print(list(result)) 


# Output : [2, 4, 6, 8]
2
#Syntax
map(functions, iterables)
2
# Python program to demonstrate working 
# of map. 
  
# Return double of n 
def addition(n): 
    return n + n 
  
# We double all numbers using map() 
numbers = (1, 2, 3, 4) 
result = map(addition, numbers) 
print(list(result))
3
# 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)) 

Output:
[5, 7, 9]
3
# Python program to demonstrate working 
# of map. 
  
# Return double of n 
def addition(n): 
    return n + n 
  
# We double all numbers using map() 
numbers = (1, 2, 3, 4) 
result = map(addition, numbers) 
print(list(result))
# result [2, 4, 6, 8]
1

    def myfunc(n):
  return len(n)

x = map(myfunc, ('apple', 
    'banana', 'cherry')) 
0

New to Communities?

Join the community