Python Tutorial – Map and Lambda Function


Hello guys and welcome to Python programming
tutorials by Amuls Academy. Today in this tutorial we are discussing about
the lambda function and map function. First we will begin with the lambda function,
lambda functions are nothing but these are the functions which are defined without a
name, that’s why it is also called as anonymous functions. Generally in Python we will define a function
using def keyword right?. For example, we will define function like
this, def followed by function name, if my function name is ‘display’ ok, next we
will mention parameter list, next this will be the function body right? This is the general Syntax of functions in
Python, but this lambda functions doesn’t contain any def keyword .
So the syntax of lambda function is, this functions are defined using the keyword lambda,
so first lambda next followed by argument list ok, so we need to mention the arguments,
it can contain one or more than one arguments next expression. As i said lambda function can contain one
or more than one arguments but it should contain only one expression ok, this is the keyword
and this lambda function returns function object and to store that function object will
use variables ok. ok so here is the example here we can see,
so this is the normal functions in Python here we can see it is started with the def
keyword, and here we can see the function name, parameters and function body.
and this is the lambda function here we can see, this is the keyword lambda and these
are the arguments here in the example there are two arguments x and y, and this is the
expression ok. So this is about the syntax of lambda. So next if you ask when we will use this lambda
functions or anonymous functions, this function can be used when we need a function for short
period of time. Next it can be used as arguments in the higher
order functions, higher order functions are nothing but the functions which will use another
function as argument so in that case we can use lambda function as the argument in the
higher order function. Next we can see the advantage of the lambda
function when it is used with the some built in functions like map reduce filter, so next
we will discuss about the map function and how this lambda function is used in that map
function, in the next tutorial we will discuss about the filter and reduce function. Ok so next is the map function, this is the
built-in function, and this function is used to apply a function to all the element of
a sequence, ok so this is little bit confusing right ? so we’ll take example before that
we will see the syntax of map function. So the syntax of map function is first we
need to mention map, ok next we need to mention the function, next here sequence. So we all know list tuple string these are
called a sequence right? so here we can use list or tuple or strings. Here this is the function so if you want apply
some function on the each element of the sequence that is list tuple or strings, then you can
use this map function ok. For example if you want to find out the square
of each element of a list, then you can use this map function. So i’ll show you the example now, so first
here i’ll show you how to use map function without using lambda function, next we will
see how to use lambda function in map function okay. Here we want to find out square of each element
of a list, so first i need to take one list ok, so i’ll take my list as ‘a’ and i’ll
take element as 1 2 3 4 ok, next in the output i want 1 square, 2 square 3 square, 4 square
that is 1 4 9 16. So for that first we need to define a function,
so here i’ll take def and i’ll take my function name as ‘square’ and i’ll take one parameter
and here in the function body i’ll write return x*x, because we want to find out the square
of that number right?, so this is the function. Next now i will use map function ok, map here
i need to mention the function first right, that is function name here my function name
is ‘square’ so square ok, next i need to mention the sequence that is on which sequence
you are applying this function, i want to apply this on the list ‘a’, so i’ll mention
a. Here we can see an iterator, here in Python
3, here we can see i’m using Python 3.5.1 here in Python 3, this map will returns the
iterator but in Python 2 map will returns the list. So in Python 3 if you want to get the output
as list, then you should use list of, then you can write map, here we can write function
name as ‘square’ and list name as ‘a’ and enter. Here we can see the output 1 4 9 16, here
if you want output in the tuple, then here you need to mention tuple ok. if you are using
python 2 then, no need to mention this list or tuple, directly you can write this function,
because in Python 2 it will return list ok. So next, ok so here we used map function without
using the lambda function, if you want to use lambda function then you can write like
this, so map ok,here only i will define the lambda function so lambda , and here i take
my argument, one argument that is ‘x’ next i’ll write expression as “x multiply
x” ok. so this is my function next i will write ‘a’ this is list. So here instead of this function, i’m using
lambda function, we will get the same output , because we are find outing the square of
each element of the list ‘a’ ok. So here i will convert this output to the
list, because i want to see the output in the list form, so this is map function using
lambda function. Here next we can use more than one list or
tuple in the map function. Suppose if you want to add two list then you
can use this map function for that. So here we can see we already have one list
called ‘a’ , next i will create another list called ‘b’. I will write 1 , 1 , 1 , 1. So while adding two list you should remember,
both the list should have same length, that is nothing but both list contains same number
of elements, and the next thing is we can’t add string and integer value right? so both list should contains numbers it can
be integer or it can be floating point value, but it should contain numbers or both list
should contain strings, so be careful about that. So now what i will do is, i will take map
and i need to mention the function right so i’ll mention lambda, here we want to add two
numbers so i need two parameter x, y and the expression will be x + y. We want two add two list, next here i’ll mention
my list as a, b ok, so now we want to convert this iterator output to list or tuple. So here i will convert my output to tuple
. ok so i will need to mention tuple there so enter, here we can see 2 3 4 5 here we
can see 1 + 1=2 , 2 + 1=3 , 3 + 1=4 , 4 + 1=5 ok this is the output. We can add list and tuple also , in the same
way we will get the output, So if you are using python 2 then i said map function will
returns the list, so we will check that ok, so here we can see i am using Python 2.7 so
i will take one list called ‘a’ and here i’ll take my element as 2 3 4 ok, so next
i will use map i will take lambda, here i’ll find out the square of each number ok. And ‘a’ so here list, we can directly
see the output, here we can see this map function will return the list in Python 2 ok, so this
is about the map function as well as lambda functions and how to use lambda function with
the map functions so that’s it for now thank you for watching don’t forget to subscribe
to my channel i will meet you in next class till then take care.

27 thoughts on “Python Tutorial – Map and Lambda Function

  1. Hi, in the square function, instead of the return, if you use yield, how to see the output? Even if i convert to a list, i am not able to see o/p in Python 3.5.2

  2. Nice explanation I don't understand with book but your video provide better understanding about lambda and map

  3. You said at the end of the video "take care " ok got it . I'll take care of myself about what about you ???😄
    (Don't take me wrong just kidding… )

Leave a Reply

Your email address will not be published. Required fields are marked *