Map and Filter Functions in Python - Python Tutorial for Absolute Beginners | Mosh

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
here's another scenario for using a lambda function so we have our list of items let's say we want to filter this list and only get the items with price greater than or equal to $10 again one basic way is to define an empty list like filtered then we iterate over our list of items for each item we get the price if it matches our criteria we'll add it to this list but that's pretty basic a better approach is to use the built in filter function look at the parameters this function just like the math function takes two parameters a function and an iterable so it will apply this function on each item of this iterable if the item matches some criteria it will return it so let's see how we can use this function as the first argument we're gonna pass a lambda function this function takes an item and returns a boolean value that determines if this item matches a criteria or not in this case we want to get the price of each item and see if it's greater than or equal to $10 so the result of this expression is a boolean value if it's true this item will be returned now as a second argument to the filter function we pass our items list let's temporarily store the result in a variable called X and print it so we get a filter object a filter object just like a map object is iterable so we can loop over it and we can also convert it to a list right away so let's call list now we get a filtered list that we can print on the terminal so as you can see we only have product 1 and 3 because their prices are greater than $10 over the next two lectures I'm going to show you more examples of using lambdas in your programs so here we have this list of items let's imagine we want to transform this list into a different shape so currently each item in this list is a topple of two items let's say we're only interested in the price of these items so we want to transform this list into a list of numbers that is the list of prices here is a basic way to do this we can define an empty list then iterate over our list of items so for item in items we call prices dot append item a1 which returns the price of each item now let's print the prices so we get a list of numbers with this code we have transformed or mapped our original list into a different list okay but there is a better and more elegant way to achieve the same result instead of this loop we can use the map function so we call the built-in map function as you can see this function takes two parameters a function and one or more iterables so as the first argument we can pass a lambda function and as the second argument we can pass our list of items this map function will apply our lambda function on each item in this list let me show you what I mean so we pass a lambda this lambda is a function that will transform each item in our original list so the parameter to this function is item and here we simply want to return the price of that item so item of one now as the second argument to the map function we need to pass our items list so this map function will iterate over is a durable and it will call this lambda function on each item of this iterable now let's see what this function returns so temporarily I'm gonna assign this to a variable called eggs let's print eggs and also we don't need these three lines because we can achieve the same result using the map function so delete now let's run this program so this map function returns a map object which is another iterable so we can easily iterate over it for item in eggs will print item there you go so we get the prices alternatively we can convert this map object into a list object so we can use our list function earlier you learned that you can pass any either herbals for this list function to create a new list so we passed it here and now we can rename X to prices and simply print prices we don't need this loop here let's run the program so we get a list of three numbers so this is how the map function works it takes a lambda function and applies it on every item of this iterable hi guys thank you for watching this tutorial my name is muhammad ani and i have tons of tutorials like this for you on my channel so be sure to subscribe and also please like and share this video if you want to learn Python properly from scratch with depth I have a comprehensive python tutorial for you the link is below this video so click the link to get started thank you and have a fantastic day
Info
Channel: Programming with Mosh
Views: 29,747
Rating: 4.9526229 out of 5
Keywords: lambda function, functional programming, python map function, python programming, programming tutorial, python tutorials, python filter function, lambda functions, python tutorial for beginners, python tutorial, code with mosh, mosh hamedani python
Id: 2qKQGqpRsks
Channel Id: undefined
Length: 5min 59sec (359 seconds)
Published: Thu Nov 15 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.