Intermediate Python Tutorial #4 - Filter() Function
Skills:
Python for Data80%
Key Takeaways
Teaches the application of the filter function in Python for data filtering and processing
Full Transcript
hey guys and welcome to the fourth video in my intermediate Python tutorials and in today's video I am going to be going over the filter function so this one is very similar to what I did in the last video which is the map function but obviously it has a few differences but the filter and map function are nice to learn back-to-back because they can be used together and a lot of people do use them together when they're making programs so I'm just gonna go ahead and start off by typing out two functions here they're gonna be using in our examples so I'm just going to make one function called add 7 which is simply going to return 7 and I'm gonna make another one which is called is odd and it's just gonna return if the number is an odd number so the way we do that again is X modulus 2 does not equal 0 like that ok pretty straightforward now I'm just gonna make a new list in this case I'm just gonna go 1 2 3 4 5 6 7 and 9 and then we'll even add a 10 in there and now I'm gonna go over what the filter function does so I want to assign a new variable here create a new one I'm just going to call this B and I'm gonna make an equal to list filter which is the name of our function just like this and now filter function actually takes the same arguments that our map function did in the last video it takes a function and it takes an iterable list so we can take a string as well but typically we just pass it a list something that's iterable and that you can go over on so what I'm gonna put in here for our first function is I'm gonna put is odd and then I'm gonna give a list a now the way filter function works is if this value so it's gonna its into the same thing the map function does is gonna pass every element in our iterable item in this case the list to the function is odd so it's gonna start with 1 it's gonna pass 1 in there you can say 1 modulus 2 does not equal 0 which is true so it's gonna give us a true value and then it's that's gonna be added to the B list because this function returned a true value now say we put 2 in here and we go and we say 2 is X 2 modules 2 well that does equal 0 so we get a false value returned here now 2 is not added to the list this is essentially filtering out based on a predefined function so obviously you can make your function that you want to check a lot longer and a lot more extensive so you're filtering out more items but this is extremely useful when you're solving problems so rather than going through a for loop and checking every single item like we might have done with the map function to add things into a list we can just simply call filter give it the function that we want to filter based on and then a list and it's gonna return that new list so let's just go ahead and run the program here and make sure that everything's working fine so I'm just gonna print out a to the screen and I'm going to print out B and we can see that B has essentially filtered out all of the elements on that we're even so we get to 6 8 10 they are all gone so now I want to show you how we can implement this with our map function why is this useful what can we use the map function for so I'm making another list here and I'm gonna call it C and this time I'm just gonna do list map and then inside a map I'm going to do filter but before filter I'm just gonna apply another function so in this case I'm going to add 7 and then in filter here I'm actually just gonna change this too because I'm gonna type the same thing out let's go over what I just did here so essentially what's gonna happen now is we're using the map function which if you don't know go back and watch the previous video and we're going to apply this new list B which has been filtered so we filtered out this original list now we have 1 3 5 7 9 so far and based on that list we're gonna add 7 to every element in that list now again if you wanted to save a line I could just get rid of B here and I could simply paste that in here removing list like that and this would work fine so let's go ahead and see what actually happens here again we're just taking this new filtered list and now we're applying another function to it in this case add 7 so to make sure that this is indeed working I haven't made a mistake here so we'll print a again and I will print C and you can see that we do indeed get that so our one-seven we get 8 are 3 add 7 we get 10 5 7 and so on you can see how this works now these are extremely straightforward examples but if you're doing a list and for example you want to filter out and the elements that contained a certain digit or that met a certain criteria then you could create a more advanced filter function function to filter based off of that had a whole bunch of criteria and then it's gonna return a true or false value so show you here if I do something like just return true and I don't return a condition per se that every element in my list is gonna be sent through because nothing's gonna be filtered out so again if I print C here all of our elements are here so we have ten elements because we're always returning true same thing if I always return false then none of our elements are gonna be printed because well it's always false so the way to think about it is uncomment this for a second is what this filter function does is it's going to apply function that gives us a true or false value to every element in the list if when that element is applied to that function we get a true value then it's going to be created in that new list which filter returns to us like that now again yeah this is really useful for solving problems and I just want to show you what happens if I do something like return one and I'll print C to the screen here you can see we get every every element out of here so pretty much the way is in Python something is said to be true as long as it's not 0 if it's a number so like if I pass something like hi well if you say is hide true technically hi is true I Canada if I print C we're still gonna get every element although I didn't pass the value true hi technically evaluates to a true value in Python the only thing that's not gonna evaluate to true value is something like 0 so now if I print C you can see we get an empty list I'm just wanted to add that and at the end there teaches something you might not have known there so anyways that's been it for the filter function in the next video I'm gonna tie these all together with something called lambdas and what they look like is that you can see it highlights here as a keyword and pretty much this is a kind of function that we can use so we don't have to keep creating all these other ones at the beginning of our program and they're extremely useful and they're really cool so make sure you guys stay tuned for the next video I'm gonna be explaining how we can use those with lists maps and on their own anyways if you guys enjoyed please make sure you leave a like and subscribe and I will see you again in the next video [Music]
Original Description
Today's Topic: filter() function, the filter function is very similar to the map() function shown in the previous video. It takes again, two arguments (function, iterable) and applies the function to all of the items in the iterable. If the function returns a True value then that item will be added to a new list.
In this set of videos I will be explaining more advanced programming concepts and showing you intermediate-advanced tools that you can use in python. A lot of these tools will help you to solve problems/code more efficiently and will save you a ton of time!
Text-Based Tutorial: https://techwithtim.net/tutorials/python-programming/intermediate-python-tutorials/filter-function/
Twitter: https://twitter.com/TechWithTimm
Future topics:
5. Lambda Functions
6. Intro to Collections
7. Collections: named tuple
8. Collections: deque
9. Collections: orderedDict
10. Collections: defualtDict
11. docstrings
Want To Support This Channel?
Bitcoin: 1PbkAYLFaJBgjbKn2ptGyBz65xWN8hJgBU
Ethereum: 0xdd42dbbdba60f7163fc7a840e189474b6e8bfcad
Ripple: rD4arM9CVjQWqi8f1kxdpCgkCgEkqBgtud
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech With Tim
- Python Tutorials
- Intermidiate Python Tutorials
- How to use the filter function in python
- filter() function
- filter in python
- filter and map function python
- Python intermediate
- Python intermediate tutorial
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 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
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: Python for Data
View skill →
🎓
Tutor Explanation
DeepCamp AI