10 Python Comprehensions You SHOULD Be Using
Key Takeaways
This video demonstrates the use of Python comprehensions, including list, dictionary, and set comprehensions, to create and manipulate data structures in a concise and efficient manner. It also covers the use of generator expressions for efficient computation and lazy evaluation.
Full Transcript
this video will show you 10 python comprehensions you can and should be using they get more and more difficult so make sure you stick around until the end of the video now let's start with the basic list comprehension so in front of me I have a basic code snippet now all this does is Loop 4X in the range 10 which means we're going to access the values 0 through 9 and we're going to append those into a new list now this is great we can do this but in Python we have a much faster way to go about writing this code and it's something that's actually a little bit easier to read now that's a list comprehension let's have a look at that so what you see on my screen now is completely equivalent to the code above what it does is use something known as a comprehension now in Python we have some really interesting syntax and one of those pieces of syntax allows us to actually write a for Loop inside of a list we can actually write it inside of a bunch of other things as well but in this case we'll just look at a list now what we do here is very similar to what we did up here we actually say that we want to take the value X for X in range 10 now what this will do is execute the for Loop just like it normally would we're going to get access to the values 0 through 9 so 0 1 2 3 Etc but for every single iteration of this for Loop what we have on the left hand side here we're going to add into this list so we're saying we want to take the value of x for X in range 10 now just to prove to you that this works if I print out my values here you see that we get 0 through 9 now let's say we change this just a little bit and we do x + 1 now this whole leftand side expression here is what's going to be added into the list for every single iteration of this for Loop so let's run the code again and notice this time we get 1 through 10 so this is the syntax it's really not that complicated whatever we have on the left hand side here is the element that we want to add for every single iter a of the for Loop now we can have all kinds of more advanced list comprehensions and I want to show you a few examples of those so now we're stepping it up a bit and we have a slightly more complicated example here what I want to do is get all of the even numbers from 0 to 50 now what I do is I write my list I Loop through the numbers in the range of 0 to 50 and then I first check if the number is even now to do that I can use the modulus operator which gives me the remainder in this case I get the remainder after dividing by two if there's no remainder so if it's equal to zero that means this number is even so I can check if it is even and then I can append it into this EV list so that's going to give me all of those values now let's see how we would do this using a list comprehension I can say evens is equal to a list and inside of here again I can say number for number in range 50 now this is going to give me all of the numbers in the range of 0 to 49 or up to 50 but not including it but what if I want a filter for the even numbers well what I can actually do is I can add a condition after the for Loop so I can put an if statement right here even though it looks crazy and it looks like we shouldn't be able to write this and I can write out the same condition I had before so I can say if number mod 2 is equal to zero now this is actually perfectly valid python syntax and what we're saying now is that we want to take the number for number in range 50 only if this condition is true so if the number mod 2 is equal to zero then we will actually place this number in the list if it's not then we won't so what I can do now is I can print out evens we can run the code and you can see that we get all of the even numbers up to but not including 50 so again it looks a little bit crazy when you look at this syntax but this is actually a really easy way to understand what's going on and what we're adding inside of a list it's a very natural way to read this we're taking the number for number in range 50 only if the number mod 2 is equal to zero now I would argue that that's easier to read than this code right here some of you may disagree with me but the reason why a lot of people love using comprehensions is because when you're populating a list it's very clear what's going on right in the line where the list is defined so now let's look at a few more advanced examples but first I want to talk to any of you that are serious about becoming software developers now look it's no secret that the job market now now is more difficult than it was a few years ago however it's still 100% possible to land a job you just need to have the right guidance and really make sure you fine-tune every component of your application and your skills now that's exactly what my program at course careers aims to do not only do we teach you everything you need to stand out from an entrylevel developer but we actually walk you through optimizing your LinkedIn setting up your resume interview best practices how to apply how to reach out to people we go the extra mile because we're really only successful if we get you a job now we've already had a ton of success even in this really tough job market so if you're considering becoming a software developer definitely check it out we have a completely free introduction course that shares a ton of value and goes over a high level overview of what you need to do in 2024 and Beyond to be able to land a software development role so now we have another example where what we want to do is filter an existing list and look for all of the strings that start with the letter a and end with the letter Y now to write that code will start by defining the list that we want to put the results in we'll then Loop through all of the strings in our options this will go through each individual string that we have the first check we do here is we see if the length of the string is less than or equal to one now the reason I check this is that if something is only one character or no characters then it means it can't start with a and end with Y and I also need to check this to make sure that if I try to access the first or last index of the string I don't get an error so that's why I have this right here so if it's less than one character or equal to one character we simply continue we move on to the next string down here we do a similar thing we say okay if the first character is not equal to a then we're going to continue because it doesn't meet the criteria and we do the same thing here we say if the last character which is at the ne -1 index is not equal to Y then we continue otherwise we can add this string into the valid strings now let's see how we would do this using a comprehension so this is one version of a comprehension we could write that would achieve the exact same goal now notice that I can actually use multiple if statements and I can kind of chain them on top of each other so very similar to what I did before I say I have string for string in my options and then I'm checking all of the conditions that I need to be true if I want to actually put this string into the resulting list now the first condition is if the length of the string is greater than or equal to two now you'll notice that I'm kind of doing it in reverse from what I did here here but that's fine it's just another way to go about writing this within the comprehension I then say if the string at index0 is equal to a and if the string at index Nega 1 so the last character is equal to Y now again this is very easy to read and understand and I've even formatted this so you can read it on multiple lines and it doesn't span out and get really difficult to read on a single line so that's one complaint some people have with list comprehensions being difficult to read if they're too long well that's really not an issue you can actually just split it out onto multiple lines as long as you have it inside of the braces or inside of the brackets now what I can do is run this code and you'll see that we get the result of any and Albany there you go now let's move on to a more advanced example so now I'll show you an example where we look at a nested list comprehension in this case let's say that we want to flatten a matrix which is a list of lists or a two-dimensional list you can see that we have some lists inside of the main list and if we wanted to flatten this so just have one array that doesn't have any nested list we need to write some code like this now it's not overly complicated but again we can really speed this up and make it a little bit cleaner using list comprehensions so here we say four row in our Matrix and then for each number within that row we're going to append that value into the flatten list now let's paste in the list comprehension and see how we would do it here you can see that it's a little bit simpler we're saying numb and then for Row in Matrix for number in row now the first four for Loop works exactly like the first for Loop here so for every single iteration of the for loop we're going to do whatever is on the right hand side here which is the next for Loop and then for every iteration of this for loop we're going to take num and we're going to add this inside of the flattened list so if I come here and I print print flattened sorry and I run this you can see that we get 1 through nine appearing in our conso I know that it can be a little bit tricky to read but just remember that the first for Loop you see is the exterior for for Loop and any for Loops after that are going to be interior ones that are going to run for every single iteration of the previous for Loop now you could keep going here and you could write as many for Loops as you want that would get pretty confusing pretty quickly but when you're only dealing with two or three of them it's totally fine to be using a nested list comprehension moving on we're looking at another example where this time we want to do something like categorize numbers as either even or odd so you can see that I have my list here for different categories what I do is Loop through all of the numbers in the range 10 so from 0 to 9 now I first check if the number is divisible by two which means it's even if it is I'm actually going to append the even string into the categories list otherwise I'm going to append the odd string now we haven't yet seen an example of how to use an if else so let's look at how we could use a list comprehension to replicate this example so there you go we now have a slightly different version of the list comprehension now notice that we still have our for Loop here the for Loop is going to be the meat of all of these comprehensions and you always want to be looking to the left and the right side of the for Loop to understand what's going on so we have our for Loop and we see that we're saying for X in range 10 now what we do here is we actually check on the left hand side what value we want to add depending on the value of x now in this instance we're not actually really filtering the list what we're doing instead is we're placing in a value for every single iteration or every single value of x so there's no need for me to put an if on the right hand side which is really meant to be filtering different values instead what I'm doing is picking what I'm going to be inserting based on the value of x so I say that I want to use even if x mod 2 is equal to zero so if x is even otherwise I want to use odd now it's worth noting that this syntax is actually valid outside of a list comprehension you could put this inside of a variable you can actually write this in a lot of places in Python but this is also perfectly valid for me to put on the left hand side of the list comprehension so for every single iteration here we're going to go to the left hand side we're going to evaluate this expression which is going to give us either even or odd based on this condition here to prove that to you I will save and run the code and you can see we get even odd even odd even odd Etc so now let's go on to something even more complicated where we need to build a three-dimensional list now you can ignore the ready printer syntax this just gives us an output that's easier to read that's why I'm using it really we just want to be looking at this now in this case what I want to build is a 5x 5x five Matrix or threedimensional list so I want a list inside of a list inside of another list and then for all of those lists I want to have values inside of them now we can look at this code here we can see that we start by building the first level of our exterior list we create list one we then for every single iteration here we're building list one we start building list two for every single iteration of list two we append values inside of list two then we put list two inside of list one then we put list one inside of the main list and we build a three-dimensional list now you don't need to fully understand it but when I run the code you see that we will get a big kind of array here or a big list of multiple lists inside of list and you can see that's a little bit confusing and difficult for us to read this code so let's see how we can clean it up a bit by using a list comprehension so you can see now that we've added the list comprehension and this immediately is a little less intimidating than this code that we have here now let's quickly run through it because I've done something a bit different than what you saw before now what we're going to start doing here is looking from the right side where we see our for Loop so the way you read this type of code is you find the first for Loop that's inside of the main list so we found that here and you can see we have four underscore in range 5 now you don't have to worry about the underscore this is just what's known as an anonymous variable and we put this in place of using something like I when I don't actually care about that value right so I don't actually need to know what iteration I'm on I just want to do this five times so I see my for Loop and we remember that whatever's on the left hand side of the for Loop is what's going to get added inside of our list so what we have here highlighted is what we'll get added but notice that inside of here I've ridden some other for Loops I've written some more comprehensions a nested comprehension now what we do is the same thing so in the left hand side I need to figure out what's going to get added so I look for the most exterior for Loop which is this one right here I can see that it's 4 underscore in range five and then I look to the left hand side now I see that I have this list and there's another comprehension inside of here so same thing I go to the next level of for Loops I see this for Loop right here and I notied that for this one I have a value numb so it says num for Num in range five and that tells me this right here is simply going to be a list that has the numbers 0 through 4 that would then tell me that what this whole statement right here is going to be is five lists that all have the numbers 0 through 4 then that tells me that what I'm going to have for this whole nested for Loop here is five lists of five lists that all have the numbers 0 through 4 now let's test it out and when I wrun the code you can see that I get exactly that so since I just showed you something really complicated let's go to something a little bit easier and this is where we apply a function to a value inside of a list comprehension now I haven't written the equivalent code because I think this is simple enough but I wanted to show you that it's perfectly valid for us to make a function call inside of a list comprehension so what I'm doing here is I'm saying okay I want to take the square of X for X in range 10 now notice how easy easy that is to read it's so nice for me to look at this and immediately know the results that I'm getting and it's helped by having really good variable names as well now obviously I could just write this as well and I could have x to the exponent 2 but imagine we have a more complex function where we don't want to write that in one line then obviously it's really nice to be able to call it now I could also use a function call in my filter so I could say if valid right and then this could be some function that returns true and I could put X obviously we haven't written that out but it' be perfectly fine for me to do that and if I have good function names and good variable names it makes this code really really clean and easy to understand and that's why we love using list comprehensions so the next few examples I have for you are a bit of a bonus because they're not list comprehensions but they're still useful to know about now the first one to look at here is the dictionary comprehension so notice that we have a kind of list of pairs or a list of Tes what we can do is we can use a for Loop inside of a dictionary which are these curly braces right here and we can say 4 K comma V in pairs now technically you can call these whatever you want but what this will do when you're accessing a tupple or that's the object that you're iterating through uh inside of a list is it will grab the first and the second value respectively so the key would be a the value would be one then what we can do is simply use the syntax key colon value just like you would normally create a dictionary now you can do some more advanced uh syntax in here as well you could like apply a function to this value if you wanted to do that or do something funky with the key but I just want to show you that dictionary comprehension does actually exist so if I print this out you'll see that if we bring up our terminal we get A1 B2 and C3 and we were able to create the dictionary comprehension so now we move on to another quick example here which is using a set comprehension so just like we have dictionary comprehensions we also have set comprehensions the main difference is you're not going to have a key here you're just going to have some value that you're adding into the set now in order to do that you can use the set syntax which is this it's just the same as the dictionary right so it's your curly braces but python will know that this should be a set if you don't have any keys right so I don't have a key associated with this it knows to treat this as a set now in this case what we're going to do is remove duplicates from a list while applying a function to those values so here this is going to give us all of the unique squares it's going to do that by squaring all of the elements inside of this nums list but then adding them into the set and since it's adding them into the set the set is only going to keep all of the unique values which means if I go here and I print out my unique squares and I run my code you can see that we only get the unique squares appearing here in the set so now we move on to our last example which is reserved for those of you that are really into Python and know about more advanced features like generators now if you don't know what a generator is I'll leave a video on the screen right here that you can watch to learn about it it's super interesting and it is actually applicable in this example so what you see here looks like a very simple comprehension but it's actually a bit more complicated than it seems what we're doing is using this sum function now what I want to do is generate the sum of all of the squares from the numbers 0 to 1 million now in order to do that I would need to have access to all of the numbers right or all of the squares from 0 to 1 million however I don't actually need to have access to all of them at the exact same time now the reason I'm bringing this up is because what some of you might do if you wanted to come up with that solution is you would start by generating all of the different values right you generate all of these different squares and you would be storing a million different values in memory now really that's not going to take up too much space in your computer but it's inefficient because we don't need to have all of these values at once we just need them one at a time so we can add them or sum them together now that's exactly what this example does right here and what a generator does in Python a generator to give you a really really quick summary only returns values when they need to be used it doesn't store a ton of different values in memory it will give you the next value when you ask for it and generate it on the fly so we're not storing all of these and pre- generating them we're only generating the value when it needs to be used now that's exactly what this example does based on the way that I've written it the range function here is going to return values one by one it's not going to generate an entire list of them so what I do here is say 4X in range 1 million and then I take the square of that value now the way that the sum function works is rather than executing this entire block right away and getting a list that contains all of the different values that we need to sum it's simply going to ask for each value sequentially and add it to a sum that it's storing internally so it doesn't need to know what the previous or the next square is it just gets access to the current square that it's looking at and then adds it to the sum that it's keeping track of so it only ever is looking at one of these values at a time and we're not storing a bunch of other values that we're not using the reason all of this works is because of features like generators and iterators and how some of these built-in functions are optimized now when we look at this right here the reason why it grabs them one by one is because I've kind of created a generator expression or a generator comprehension however if I were to actually put the list syntax here this would completely change now what I would be doing is what I talked about before where I would actually generate every single possible value or every single Square in the range 1 to 1 million or 0 to 1 million and store it in a list and then I would pass that list to sum now that's not what we want so instead we want to remove this but I just want to show you that if you were to Simply add this list syntax now you've created a list comprehension and you will generate all of these values before the sum function is actually applied whereas when you remove this you're using something known as a generator comprehension which I'm going to encourage you to read a little bit more about if you're interested in anyways with that said I'm going to wrap up the video here I hope you found this helpful and I look forward to seeing you in the next one [Music] oh
Original Description
To learn programming and Python - check out Datacamp!
💻 Learn Python - https://datacamp.pxf.io/anvmQo
💻 Learn Programming - https://datacamp.pxf.io/k0D3G3
In this video, I will be showing you 10 Python comprehensions you can & should be using. These comprehensions will progress in difficulty, so be sure to stick around until the end of the video!
If you want to land a developer job: https://techwithtim.net/dev
⏳ Timestamps ⏳
00:00 | Overview
00:11 | Basic List Comprehensions
02:06 | Comprehension Condition
04:22 | If You're Serious About Becoming A Developer
05:20 | Comprehension With Multiple Conditions
07:42 | Multiple List Comprehension
09:18 | If/Else In A Comprehension
11:22 | Nested List Comprehension
14:32 | Transformation In Comprehension
15:38 | Dictionary Comprehension
16:47 | Set Comprehension
17:49 | Generator Comprehension
Hashtags
#techwithtim
#programming
#python
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: AI Pair Programming
View skill →Related Reads
Chapters (12)
| Overview
0:11
| Basic List Comprehensions
2:06
| Comprehension Condition
4:22
| If You're Serious About Becoming A Developer
5:20
| Comprehension With Multiple Conditions
7:42
| Multiple List Comprehension
9:18
| If/Else In A Comprehension
11:22
| Nested List Comprehension
14:32
| Transformation In Comprehension
15:38
| Dictionary Comprehension
16:47
| Set Comprehension
17:49
| Generator Comprehension
🎓
Tutor Explanation
DeepCamp AI