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

Programming with Mosh · Beginner ·🛠️ AI Tools & Apps ·7y ago

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 6 Visual Studio Tips to Increase Your Productivity | Mosh
6 Visual Studio Tips to Increase Your Productivity | Mosh
Programming with Mosh
2 Visual Studio Keyboard Shortcuts that Speed Up Debugging Applications | Mosh
Visual Studio Keyboard Shortcuts that Speed Up Debugging Applications | Mosh
Programming with Mosh
3 Backbone.js Tutorial Part 2 - Backbone.js Models: Working with Model Attributes
Backbone.js Tutorial Part 2 - Backbone.js Models: Working with Model Attributes
Programming with Mosh
4 Backbone.js Tutorial Part 3 - Backbone.js Models: Model Validation
Backbone.js Tutorial Part 3 - Backbone.js Models: Model Validation
Programming with Mosh
5 Backbone.js Tutorial Part 4 - Backbone.js Models: Model Inheritance
Backbone.js Tutorial Part 4 - Backbone.js Models: Model Inheritance
Programming with Mosh
6 Backbone.js Tutorial Part 1 - Backbone.js Models: Creating Models
Backbone.js Tutorial Part 1 - Backbone.js Models: Creating Models
Programming with Mosh
7 Backbone.js Tutorial Part 5 - Backbone.js Models: Syncing Models with the Server
Backbone.js Tutorial Part 5 - Backbone.js Models: Syncing Models with the Server
Programming with Mosh
8 Backbone.js Tutorial Part 6 - Backbone.js Collections: Creating Collections
Backbone.js Tutorial Part 6 - Backbone.js Collections: Creating Collections
Programming with Mosh
9 Backbone.js Tutorial Part 7 - Backbone.js Collections: Working with Collections
Backbone.js Tutorial Part 7 - Backbone.js Collections: Working with Collections
Programming with Mosh
10 Backbone.js Tutorial Part 8 - Backbone.js Collections: Fetching Collections from the Server
Backbone.js Tutorial Part 8 - Backbone.js Collections: Fetching Collections from the Server
Programming with Mosh
11 Backbone.js Tutorial Part 9 - Backbone.js Views: Creating Views
Backbone.js Tutorial Part 9 - Backbone.js Views: Creating Views
Programming with Mosh
12 Backbone.js Tutorial Part 10 - Backbone.js Views: Passing Data to Views
Backbone.js Tutorial Part 10 - Backbone.js Views: Passing Data to Views
Programming with Mosh
13 Backbone.js Tutorial Part 11 - Backbone.js Views: Handling the DOM Events
Backbone.js Tutorial Part 11 - Backbone.js Views: Handling the DOM Events
Programming with Mosh
14 Backbone.js Tutorial Part 12 - Backbone.js Views: Handling the Model Events
Backbone.js Tutorial Part 12 - Backbone.js Views: Handling the Model Events
Programming with Mosh
15 Backbone.js Tutorial Part 13 - Backbone.js Views: Handling Collection Events
Backbone.js Tutorial Part 13 - Backbone.js Views: Handling Collection Events
Programming with Mosh
16 Backbone.js Tutorial Part 14 - Backbone.js Views: Templating
Backbone.js Tutorial Part 14 - Backbone.js Views: Templating
Programming with Mosh
17 Clean Code: Learn to write clean, maintainable and robust code
Clean Code: Learn to write clean, maintainable and robust code
Programming with Mosh
18 C# Events and Delegates Made Simple | Mosh
C# Events and Delegates Made Simple | Mosh
Programming with Mosh
19 C# Generics Tutorial: Whats and Whys | Mosh
C# Generics Tutorial: Whats and Whys | Mosh
Programming with Mosh
20 Debugging C# Code in Visual Studio | Mosh
Debugging C# Code in Visual Studio | Mosh
Programming with Mosh
21 Repository Pattern with C# and Entity Framework, Done Right | Mosh
Repository Pattern with C# and Entity Framework, Done Right | Mosh
Programming with Mosh
22 Angular 2 Tutorial for Beginners: Learn Angular 2 from Scratch | Mosh
Angular 2 Tutorial for Beginners: Learn Angular 2 from Scratch | Mosh
Programming with Mosh
23 Architecture of Angular 2+ Apps
Architecture of Angular 2+ Apps
Programming with Mosh
24 Working with Components in Angular
Working with Components in Angular
Programming with Mosh
25 C# Tutorial For Beginners - Learn C# Basics in 1 Hour
C# Tutorial For Beginners - Learn C# Basics in 1 Hour
Programming with Mosh
26 Difference between Junior and Senior Developers
Difference between Junior and Senior Developers
Programming with Mosh
27 Step-by-step ASP.NET MVC Tutorial for Beginners | Mosh
Step-by-step ASP.NET MVC Tutorial for Beginners | Mosh
Programming with Mosh
28 [Pluralsight]: Become a Full-stack .NET Developer
[Pluralsight]: Become a Full-stack .NET Developer
Programming with Mosh
29 Xamarin Forms Tutorial: Build Native Mobile Apps with C#
Xamarin Forms Tutorial: Build Native Mobile Apps with C#
Programming with Mosh
30 Value Types and Reference Types in JavaScript
Value Types and Reference Types in JavaScript
Programming with Mosh
31 Using Redux in Angular 2+ Apps | Mosh
Using Redux in Angular 2+ Apps | Mosh
Programming with Mosh
32 Testing Angular 2+ Apps with Jasmine and Karma | Mosh
Testing Angular 2+ Apps with Jasmine and Karma | Mosh
Programming with Mosh
33 Profile and optimize your Angular 2 apps
Profile and optimize your Angular 2 apps
Programming with Mosh
34 Build a Real-world App with ASP.NET Core and Angular 2
Build a Real-world App with ASP.NET Core and Angular 2
Programming with Mosh
35 Entity Framework 6 Tutorial: Learn Entity Framework 6 from Scratch
Entity Framework 6 Tutorial: Learn Entity Framework 6 from Scratch
Programming with Mosh
36 Two-way Binding and ngModel in Angular 4
Two-way Binding and ngModel in Angular 4
Programming with Mosh
37 Udemy Live 2017: Teaching Tech Panel
Udemy Live 2017: Teaching Tech Panel
Programming with Mosh
38 Demo of An E-commerce App Built with Angular, Firebase and Bootstrap 4
Demo of An E-commerce App Built with Angular, Firebase and Bootstrap 4
Programming with Mosh
39 My Brand New Angular Course
My Brand New Angular Course
Programming with Mosh
40 TypeScript Tutorial - TypeScript for React - Learn TypeScript
TypeScript Tutorial - TypeScript for React - Learn TypeScript
Programming with Mosh
41 Access Modifiers in TypeScript
Access Modifiers in TypeScript
Programming with Mosh
42 TypeScript Interfaces
TypeScript Interfaces
Programming with Mosh
43 TypeScript Classes
TypeScript Classes
Programming with Mosh
44 TypeScript Constructors
TypeScript Constructors
Programming with Mosh
45 TypeScript Properties
TypeScript Properties
Programming with Mosh
46 Angular Tutorial for Beginners: Learn Angular & TypeScript
Angular Tutorial for Beginners: Learn Angular & TypeScript
Programming with Mosh
47 AngularJS vs Angular 2 vs Angular 4 | Mosh
AngularJS vs Angular 2 vs Angular 4 | Mosh
Programming with Mosh
48 Angular Material Tutorial | Mosh
Angular Material Tutorial | Mosh
Programming with Mosh
49 Angular Animations Tutorial | Mosh
Angular Animations Tutorial | Mosh
Programming with Mosh
50 Firebase in Angular Applications | Mosh
Firebase in Angular Applications | Mosh
Programming with Mosh
51 Deploying Angular Applications | Mosh
Deploying Angular Applications | Mosh
Programming with Mosh
52 Building Forms in Angular Apps | Mosh
Building Forms in Angular Apps | Mosh
Programming with Mosh
53 Directives in Angular Applications
Directives in Angular Applications
Programming with Mosh
54 Routing and Navigation in Angular | Mosh
Routing and Navigation in Angular | Mosh
Programming with Mosh
55 Angular 4 in 40 Minutes
Angular 4 in 40 Minutes
Programming with Mosh
56 [NEW COURSE] Unit Testing for C# Developers
[NEW COURSE] Unit Testing for C# Developers
Programming with Mosh
57 Unit Testing C# Code - Tutorial for Beginners
Unit Testing C# Code - Tutorial for Beginners
Programming with Mosh
58 C# Classes Tutorial | Mosh
C# Classes Tutorial | Mosh
Programming with Mosh
59 C# Object Initializers Tutorial
C# Object Initializers Tutorial
Programming with Mosh
60 C# Constructors Tutorial | Mosh
C# Constructors Tutorial | Mosh
Programming with Mosh

This tutorial teaches how to use map and filter functions in Python to transform and filter lists of items, with a focus on lambda functions and iterables.

Key Takeaways
  1. Define a list of items to be transformed or filtered
  2. Use the map function to apply a lambda function to each item in the list
  3. Use the filter function to apply a lambda function to each item in the list and return a new list with only the items that match the criteria
  4. Convert the resulting map or filter object to a list using the list function
  5. Print the resulting list to verify the transformation or filtering
💡 The map and filter functions in Python can be used with lambda functions to efficiently transform and filter lists of items, making it a powerful tool for data manipulation.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →