This article describes the basic python Tutorial-related information on Map usage. A simple example is provided here. For more information, see
Python Map
Map maps a function to all elements in an input list. Map specifications: map (function_to_apply, list_of_inputs)
Most of the time, we need to pass all the elements in the list one by one to a function and collect the output. For example:
items = [1, 2, 3, 4, 5] squared = [] for i in items: squared.append(i**2)
Using Map allows us to solve this problem in a simpler way.
items = [1, 2, 3, 4, 5] squared = list(map(lambda x: x**2, items))
Most of the time, we use the anonymous function lambda in python to work with map. We can use a list function as well as a list input.
def multiply(x): return (x*x) def add(x): return (x+x) funcs = [multiply, add] for i in range(5): value = list(map(lambda x: x(i), funcs)) print(value)
The above program output is:
# Output: # [0, 0] # [1, 2] # [4, 4] # [9, 6] # [16, 8]
Thank you for reading this article. I hope it will help you. thank you for your support for this site!
For more articles about how to use Map in the basic python tutorial, refer to PHP!