Basic python tutorial-how to use Map, basic python tutorial map
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!