#43 Python Tutorial for Beginners | Filter Map Reduce

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is 720 and let's continue the series on Python now in the last video we have talked about lambdas wide we have talked about anonymous functions this will not confirm very well use it of course we know the concept that we have a function without name it should have only one expression it can it can take multiple arguments but hold on ready will use it so in this video we will use three functions one is filter map and radios okay and let's try to use this lambda there and you will understand the importance of it so to understand this what I will do is I will take an example here the example is let's see if I have a list of values I will send dumps and in this nums I have certain values let me just drop this those values here so we got this value 0 ok I don't know how many numbers are even or odd of course we can do some complex example here but just to make it simple just to keep it simple let's go with this so we got this number I just want to check how many numbers are even in fact I want to fetch all the even numbers from this list so I'm going to get a new list which is evens and in this evens ok I don't know if it is a good name even but doesn't matter so in this evens I want to assign all the even numbers ok how do you do that of course you have to write a function right and then we have inbuilt function in Python called as freedom ok so this thing we have this functions so what this filter will do is it will take the list from you it will filter this list it will give you the values so you will say hey I want you what numbers will say will say ok done okay so filter will take the sequence and it will give you a sequence but we want a list in return right so this filter will give you something I want a list out of it and that should work but this is one problem here when you say filter how it fills don't know what exact what values you want because see we will pass none that's perfectly right that's what we will pass nums to our filter test sequence right that's a list but then how it will return so what should be the condition right and that's why filter says hey you give me function if I say control space here for filter so filter takes two arguments the first one is function and second one is it tribal which is sequence in this case but what function we are talking about so what do you do is you have to say is even function so we are passing is even but is it inbuilt function of course not so that means we to define this function so I will say def I will define this function is even I just went with this name because we do we are not able to record any name here and this is even takes a value let's say the values a dot m and this will return true or false if that n is even number it will return true if that n is odd number it will append false example let us say if I give you these stuff phones and cell phones are Android phones and some phones are non Android phones maybe Windows Phone iOS or BlackBerry's so if I give you those phones your job is to filter it ok so I would say hey filter based on Android not Android so you will take the phone and you will say ok this is Android kept it here this is not Android keep it there so this is what we are doing so in fill it out this is what it does so it is true or false depend upon true it will give it the value it depend up if it is false it will not give you the value so will say return I mod 2 is equal to 0 ok so if it is not I n how can I say I so I will say n mod 2 is equal to 0 that's it ok so this function returns a value true or false based on this expression right and they should work now because if I print this even's see the output it gives you all the even numbers so in the list you do have odd numbers or unfortunately we have only toward now but about that's fine okay so this even numbers you can see if you even's we only have even numbers now so this thing filter takes two arguments the first one is the function which where you will mention the logic and second one is the list or the sequence but don't you think this function here is only to be used here right see normally when you talk about functions we use functions so that it can be reusable but look at this function don't you think this is a very customized function only for that purpose and it won't returns one values in that case you can use lambda so you want to know how to use lambda here it's very simple we don't need this function because in this function we need only two things the variable name N and the expression right so I don't want this in this so instead of using is even will mention the function here it's so you have to say lambda space mention the variable languages and/or any variable mu for that matter give a : and mention the operation which is n mod 2 is equal to equal to 0 that's this D are defining a function so you're saying so this is my sequence which is least in this case and this is my function based on that filter it and it works ok so that's the beauty about lambda so you can reduce the number of lines you are using and it makes your court look cool right so that's what programmers do right making things cool now let's try map so let's say we got all the even numbers right so in this we got all the even numbers now my job is to double all the values so whatever even numbers you caught I want to double it or maybe I want to let's say 2 plus 2 so whatever even numbers we have I want to say plus 2 so even numbers are 2 6 8 I want to say plus 2 so 2 should be 4 6 should be 8 8 should be 10 so that's what I want to do now anyway you want to change every value we can use very simple concept for this map okay now if you don't know if you have if you have ever heard about big data concepts right say when I was learning Big Data for the first time was when I was doing doing something with Google's MapReduce concept this is what we do you know so we take the chunk of data and then we try to filter that data and then we map it and then we try to reduce it okay so this same concept can be implemented here so if you have a bunch of values filter out the values which you don't need and then apply some operation with the help of maps so map what map does is it takes the value apply some operation maybe plus 2 in multiply depend upon what you want and then you can also use the reduce function which we'll see later so if I give you a chunk of data let's say if you want to add all the values reduce if you want to multiply all the values videos if you want to find one value out of the chunk you will use videos it can be some it can get in any other logic how maybe Everage right so here what I will do is I want to find I want to double all the values I would say doubles right so we got events and then we want to find doubles of it so of course I want in the least format and this time I will not I don't want to reiterate I want a map I want to change all the values so I will say map and in this I will pass evens but then when you say map events you have to also pass one more thing because map takes two arguments so map takes a function and a table again the same problem function now what function I'm talking about is the logic right so we can do that here we can define a function we will say update because it's getting this value it will take N and whatever do is you have to just add two right so you will say return n plus two so whatever value are getting do you think they say n plus two right but and then we have to pass that update function here right so map takes two arguments the function and the sequence right they are doing that here but there's only one thing right I don't want to use this function because the only purpose of this function is to work with map right we will not be using this function anymore so in that case it's something that they're here itself we can say lambda as we did before we can say lambda and : n plus 2 right it's so simple you can simply create a nonnamous function or lambda here itself right so this works we got double and the last one is I want to perform reduce I want to reduce all the values example if I print this double first let me just print doubles so this doubles you will get the same stuff but then you are saying plus 2 oh we were supposed to double right so I have to say in to do not thus 2 in fact you're lodging right so you want to double it or you want to say plus 2 but since that I by mistake I admit I have taken the name double so I will make it double let's run this and you can see record for 12 16 8 12 and 4 that perfectly works but now what am I do is I want to reduce the banjo so instead of having multiple values I want only one value I want to add all these values right not technically when you add values how we do it is you simply say ok 4 plus 12 is 16 16 plus 16 is 32 32 plus 8 is 40 40 plus 12 is I mean this goes on right so you at one time you only add two values even if I give you ten values you will add two values at a time that's important so how do you do it and that's where we will use reduce services sum is equal to of course I'm not expecting multiple values here and expecting only one value that's why I will not be using least some things our function called as videos but if the author a city you can see it says no suggestion that means we are not able to use videos the thing is this radius function it belongs to a module called as function to tool so I would say from Fung Fung stools import videos because we want to work with the deuce right and then we can say now we can perform this operation so we can say reduce and now reduce if I say control space oh let us also takes two things okay that thing is we have to pass two things like the first one is the function okay the logic of course because it depend upon what you wanna do maybe you want to add all the numbers maybe you want to multiply out the numbers so you have to pass a function and then you want to pass a sequence so sequence is there in doubles because I want to add all the values of doubles but I also need to specify okay what I did I just made a mistake there okay and then this you have to pass a function now which what the what function I'm talking about so what I will do is here I will create a function which is deaf and I will call this function as ad all of course you can use any function name and this will take two parameters okay so when you say a function for for a deuce it will always take to put on two values at a time so I will say a comma B and here with those two values it doesn't matter what your gonna do you always stick to another time so you will say a plus B depend upon what logic you write it will it will do that and here you have to pass add all okay that's what you do so in reduce you take you pass two parameters function and sequence and now my show pending double print some okay we got and other okay my bad okay so I am doing radius insider to use videos I've had what's wrong with me okay you can see we got the aqua which is 56 okay you have to trust me at this point it should be 56 so now let's use lambda here right so instead of doing this let's use lambda so in lambda what you will do is you will again you should use using and all you will say lambda you now you need two parameters right you will say a comma B and he will say a plus B so it will take two values at a time so when you say you have doubles let me also put in doubles here because I want to see I want to show you how it will add those two values and we don't need add all function now let's run this code and you can see it was it is finally double of all this it is fine it is adding all these values right at one time it will add four and 12 which is 16 and then that system will be added with another 16 right and then it will added with eight and then twelve and then four right that's a sequence it follows so at one point it will take two manuals and that reduces doing so that's how we work with filter map and reduce with the help of lambda okay you get a lambda expression all under function your choice so I hope you understood something from this topic try it out practice because this is not that easy so let me know in the comment section if I facing some issues and do subscribe for further videos bye bye you
Info
Channel: Telusko
Views: 238,691
Rating: 4.9468656 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python
Id: kj850Y8y8FI
Channel Id: undefined
Length: 11min 34sec (694 seconds)
Published: Tue Aug 07 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.