Map and Filter Functions in Python - Python Tutorial for Absolute Beginners | Mosh
Skills:
Python for Data80%
Key Takeaways
This video tutorial covers the use of map and filter functions in Python, including lambda functions, to transform and filter lists of items.
Full Transcript
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
Original Description
Confused about map, filter and lambda functions in Python? Watch this Python tutorial.
🔥Subscribe for more Python tutorials like this: https://www.youtube.com/channel/UCWv7vMbMWH4-V0ZXdmDpPBA?sub_confirmation=1
🔥Python for Beginners Tutorial: https://youtu.be/yE9v9rt6ziw
Python Exercises for Beginners:
https://programmingwithmosh.com/python/python-exercises-and-questions-for-beginners/
Python Cheat Sheet:
https://programmingwithmosh.com/python/python-3-cheat-sheet/
Want to learn more from me? Check out my blog and courses:
http://programmingwithmosh.com
https://www.facebook.com/programmingwithmosh/
https://twitter.com/moshhamedani
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Programming with Mosh · Programming with Mosh · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
6 Visual Studio Tips to Increase Your Productivity | Mosh
Programming with Mosh
Visual Studio Keyboard Shortcuts that Speed Up Debugging Applications | Mosh
Programming with Mosh
Backbone.js Tutorial Part 2 - Backbone.js Models: Working with Model Attributes
Programming with Mosh
Backbone.js Tutorial Part 3 - Backbone.js Models: Model Validation
Programming with Mosh
Backbone.js Tutorial Part 4 - Backbone.js Models: Model Inheritance
Programming with Mosh
Backbone.js Tutorial Part 1 - Backbone.js Models: Creating Models
Programming with Mosh
Backbone.js Tutorial Part 5 - Backbone.js Models: Syncing Models with the Server
Programming with Mosh
Backbone.js Tutorial Part 6 - Backbone.js Collections: Creating Collections
Programming with Mosh
Backbone.js Tutorial Part 7 - Backbone.js Collections: Working with Collections
Programming with Mosh
Backbone.js Tutorial Part 8 - Backbone.js Collections: Fetching Collections from the Server
Programming with Mosh
Backbone.js Tutorial Part 9 - Backbone.js Views: Creating Views
Programming with Mosh
Backbone.js Tutorial Part 10 - Backbone.js Views: Passing Data to Views
Programming with Mosh
Backbone.js Tutorial Part 11 - Backbone.js Views: Handling the DOM Events
Programming with Mosh
Backbone.js Tutorial Part 12 - Backbone.js Views: Handling the Model Events
Programming with Mosh
Backbone.js Tutorial Part 13 - Backbone.js Views: Handling Collection Events
Programming with Mosh
Backbone.js Tutorial Part 14 - Backbone.js Views: Templating
Programming with Mosh
Clean Code: Learn to write clean, maintainable and robust code
Programming with Mosh
C# Events and Delegates Made Simple | Mosh
Programming with Mosh
C# Generics Tutorial: Whats and Whys | Mosh
Programming with Mosh
Debugging C# Code in Visual Studio | Mosh
Programming with Mosh
Repository Pattern with C# and Entity Framework, Done Right | Mosh
Programming with Mosh
Angular 2 Tutorial for Beginners: Learn Angular 2 from Scratch | Mosh
Programming with Mosh
Architecture of Angular 2+ Apps
Programming with Mosh
Working with Components in Angular
Programming with Mosh
C# Tutorial For Beginners - Learn C# Basics in 1 Hour
Programming with Mosh
Difference between Junior and Senior Developers
Programming with Mosh
Step-by-step ASP.NET MVC Tutorial for Beginners | Mosh
Programming with Mosh
[Pluralsight]: Become a Full-stack .NET Developer
Programming with Mosh
Xamarin Forms Tutorial: Build Native Mobile Apps with C#
Programming with Mosh
Value Types and Reference Types in JavaScript
Programming with Mosh
Using Redux in Angular 2+ Apps | Mosh
Programming with Mosh
Testing Angular 2+ Apps with Jasmine and Karma | Mosh
Programming with Mosh
Profile and optimize your Angular 2 apps
Programming with Mosh
Build a Real-world App with ASP.NET Core and Angular 2
Programming with Mosh
Entity Framework 6 Tutorial: Learn Entity Framework 6 from Scratch
Programming with Mosh
Two-way Binding and ngModel in Angular 4
Programming with Mosh
Udemy Live 2017: Teaching Tech Panel
Programming with Mosh
Demo of An E-commerce App Built with Angular, Firebase and Bootstrap 4
Programming with Mosh
My Brand New Angular Course
Programming with Mosh
TypeScript Tutorial - TypeScript for React - Learn TypeScript
Programming with Mosh
Access Modifiers in TypeScript
Programming with Mosh
TypeScript Interfaces
Programming with Mosh
TypeScript Classes
Programming with Mosh
TypeScript Constructors
Programming with Mosh
TypeScript Properties
Programming with Mosh
Angular Tutorial for Beginners: Learn Angular & TypeScript
Programming with Mosh
AngularJS vs Angular 2 vs Angular 4 | Mosh
Programming with Mosh
Angular Material Tutorial | Mosh
Programming with Mosh
Angular Animations Tutorial | Mosh
Programming with Mosh
Firebase in Angular Applications | Mosh
Programming with Mosh
Deploying Angular Applications | Mosh
Programming with Mosh
Building Forms in Angular Apps | Mosh
Programming with Mosh
Directives in Angular Applications
Programming with Mosh
Routing and Navigation in Angular | Mosh
Programming with Mosh
Angular 4 in 40 Minutes
Programming with Mosh
[NEW COURSE] Unit Testing for C# Developers
Programming with Mosh
Unit Testing C# Code - Tutorial for Beginners
Programming with Mosh
C# Classes Tutorial | Mosh
Programming with Mosh
C# Object Initializers Tutorial
Programming with Mosh
C# Constructors Tutorial | Mosh
Programming with Mosh
More on: Python for Data
View skill →
🎓
Tutor Explanation
DeepCamp AI