Learn Python like a Professional! Take You from 0 to Hero
In this Python lecture, we will learn about the Map( ).
"Map is the function that takes a function and an iterable as a parameter returns a new iterable ".
Map( ) in Python
Let's see example of Map( )-
def cube_function(num): return num**3
cube_function(2)
Now let's apply the cube_function on the element of the list
l=[1,2,3,4,5]
list(map(cube_function,l))
We could use a lambda expression instead of cube_function
list(map(lambda num:num**3,l))
Let's try some other iterable
list(map(lambda num:num**3,(1,2,3,4,5)))
In order to get tuple output, we have to put map function inside tuple function
tuple(map(lambda num:num**3,(1,2,3,4,5)))
We could also use two iterable in the map function
list(map(lambda a,b:a+b,[1,2,3],[4,5,6]))
tuple(map(lambda a,b:a+b,(1,2,3),(4,5,6)))
EDITS ARE WELCOMED!!
In the next Blog, we will discuss Reduce( )
https://sngurukuls247.blogspot.com/2018/12/python-ninja-bootcamp-33-reduce-function.html
......................................................................................................................................
Instagram-
https://www.instagram.com/python.india/
View the Jupyter Notebook for this lecture
Download the Jupyter Notebook for this lecture
https://sngurukuls247.blogspot.com/2018/12/python-ninja-bootcamp-33-reduce-function.html
......................................................................................................................................
Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnwInstagram-
https://www.instagram.com/python.india/
View the Jupyter Notebook for this lecture
Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com
No comments:
Post a Comment