Monday, December 10, 2018

Python Ninja Bootcamp 32-Map( )


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


Now let's apply the cube_function on the element of the list

l=[1,2,3,4,5]

list(map(cube_function,l))
>>[1,8,27,64,125]

We could use a lambda expression instead of cube_function

list(map(lambda num:num**3,l)) 
>> [1,8,27,64,125]


Let's  try some other iterable

list(map(lambda num:num**3,(1,2,3,4,5)))
>> [1,8,27,64,125]


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)))
>> (1,8,27,64,125)

We could also use two iterable in the map function

list(map(lambda a,b:a+b,[1,2,3],[4,5,6]))
>> [5,7,9] 


tuple(map(lambda a,b:a+b,(1,2,3),(4,5,6)))
>> (5,7,9)


EDITS ARE WELCOMED!!

In the next Blog, we will discuss Reduce( ) 

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-_1R1xnw

Instagram-
https://www.instagram.com/python.india/

View the Jupyter Notebook for this lecture

Download the Jupyter Notebook for this lecture 



Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

No comments:

Post a Comment