Python Tutorial: Comprehensions - How they work and why you should be using them

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

A good tip when learning comprehensions, or simply making them more readable, is to wrap them:

[n * n
 for n in nums]

This technique helps more as code becomes more complex. The following would look horrible on a single line but is pretty neat when wrapped:

[line.lower()[::-1]
 for line in file_obj
 if not line.startswith('_')]
👍︎︎ 2 👤︎︎ u/mdond 📅︎︎ Aug 25 2015 🗫︎ replies

This is a nice summary of [t + '-comprehensions' for t in ['list', 'dict', 'set']] but he seems a little bit too opinionated about higher-order functions. map, filter and reduce may be a bit complicated for the uninitiated, but once you comprehend list comprehensions and anonymous functions you also get these.

Also: rimshot.

👍︎︎ 1 👤︎︎ u/hutcherino 📅︎︎ Aug 25 2015 🗫︎ replies
Captions
hey everybody how's it going in this video we're going to be taking a look at list comprehensions in Python so basically a list comprehension is an easier and more readable way to create a list now I think the best way to learn list comprehensions is to first show what it would look like in a for loop because I think everybody is familiar with for loops and even if you're coming from another language you'll be familiar with that as well so let's take a look at some of these examples and I'll show you some of the advantages to list comprehensions in terms of how easy they are to write and also in how easy they are to read so let's go ahead and take a look at this first example here so in this first example I have this comment here that this is just going to be the easiest loop all it does is pretty much copy it so I'm saying that I want in for each in and nums so if you're going to write this in a for loop first you would create an empty list and then you see here I have a list of numbers 1 through 10 and then I have this empty list my nums so we're doing 4 in and nums so it's going to loop through all these numbers my list dot append in so it's just going to add each number to this list now we're going to print out that list so let's go ahead and run that and you can see that it's exactly what we'd expect it's just a copy of the top list here now let's see what this looks like in a list comprehension and also notice how similar the list comprehension is to my comment here so what I'm going to say is I'm going to make my list equal to and then brackets so the brackets means that we're making a list and then let's try to pretty much copy exactly what is in the comment here so I want in and then for each in in nums that's a list comprehension so what so this is what we're returning this is what we want I want in and then this is the for loop here for in and nums so if I save that and run it then I actually have to print this out so that it shows up so we'll run that and you'll see that it's the exact same result that we had from the for loop but this is much easier right and it's pretty easy to understand also so that's about the easiest list comprehension that you can do so let's do a slightly more complicated example so in this example here if we read the comment I'm saying that I want N squared so n times n for each N and nums so this is pretty similar to our previous example but now we're actually returning the square of n instead of just copying all the values so to do this in a for loop we would create an empty list and then we would loop through all the numbers and then we would append the square to the list and then when we're done we can print the list so if I run this code then you can see that our result we get all of the squares of each number in this 1 through 10 list okay now let's see the same example but in a list comprehension and also let's notice how similar it is to the comment again so it's almost like reading exactly what we want so we want n times n for n in nums so this is what we are appending to our list and this is the for loop here so if I save that and then let me remember to print it out this time and then run it then we get the exact same result as our for loop now there's another way to do something like this that's very similar if you know how to use maps and lambdas then maybe you've made something like this before so let me comment out this code so map pretty much runs everything in the list through a certain function and lambda is an anonymous function so if I save this and print it out you can see that we got the same result but this comprehension is pretty much do away with these map functions because they're no longer needed this works and it's a one-liner just like the comprehension but you can't read this and understand exactly what it's doing unless you already knew about these to begin with if you compare this readability to the list comprehension somebody who's completely new to Python can see this and say n x in for in and nums and that just is readable it just kind of flows together and you can kind of understand what's going on with these maps and lambdas it says map lambda in n times in nuns it just doesn't it just doesn't read very well and you have to be experienced with those before you understand what's going on so if you are using maps and lambdas from within your code then you might want to check and see if those can be converted to list comprehensions because 99% of the time they can be so I'm going to comment these out here and erase these and we'll move on to a slightly more difficult example okay so in the comment for this example I'm saying that I want n for each n and nums if n is even so this is going to create a list that is all the even numbers of our original 1 through 10 list so what's going on in our for loop here is we're creating our empty list and then we're saying for each item in the numbers list then if that number mod 2 which will give us the remainder after we divided by 2 is equal to 0 that means that it's even so then append that to our list and then at the end we'll print out a print out our list so if we run that and you can see that it works we get 2 4 6 8 10 so now let's look at this same example using a list comprehension so I'm going to say my list equal to brackets which means that we're making a list so we want in for n in nums if in mod 2 equals equals 0 so what we're saying is that we want we want an item for each item and nums if that item two equals equals zero so let's save that and actually let me print it out here and then run that and you can see that we get the exact same result as our for loop now just like the map and lambda functions there's also the filter and lambda functions which is pretty much the same thing so instead of Matt we have filter and what this does is it filters it runs the list through this function and only gives us the values that are even so if we print this out you can see that it works but the same with the map lambda function people just aren't going to know what this is if they're new to it it's just not as readable as this list comprehension is so let's go ahead and delete all of this stuff and move on to a slightly more difficult example so in this example here I'm saying that I want a letter number pair for each letter an ABCD and each number in 0 1 2 3 so for example I would want a 0 a 1 a 2 a 3 B 0 B 1 B 2 B 3 and so on so if we were to do this with a for loop then we would create our empty list then we would do a for loop for letter in ABC and then within that for loop we're going to have another nested for loop and say for num and rage 4 which will give us 0 1 2 3 and then we're going to do a my list dot append a tuple of the pair letter and number so if I save that and I run it then you can see that we got all of our pairs we got a 0 a 1 a 2 a 3 B 0 B 1 B 2 B 3 and so on so we can have these nested for loops in list comprehensions as well so if I was to do this in a list comprehension I would say my list equals create an empty list here and now what I want is I want letter num 4 letter in ABCD and then right after that for loop for num in range 4 so let's save this and print it out and make sure I typed everything right so we print that out and we see that we get the exact same result as our for loop now let's walk through this one one more time so what we're saying is that we want a list so we open up our brackets and we're saying that we want this tuple letter number for letter and ABCD for num and range for so one thing I probably should have mentioned earlier is that these values that were saying that we want they have to match these values that we assign here so I couldn't just call this number here I couldn't say that I want letter num for letter and ABCD for number and range for because it's not going to know what this value is this num value we have to use the same values that we want whenever we say that we want those values in the list so you can see how we can start to do some complicated lists here that take you know a good bit of lines of code that we can write as a one-liner and it's slightly more readable as well but it's not only lists that you can do these comprehensions with you can also do this with dictionaries and sets also so if I delete that then let's move down to our next example here now this is going to be an example of a dictionary comprehension so I have two lists here I have names and then I have their superhero name that matches up with their first name so if you've never seen this zip function here let me just print out this zip function here so that you know what it does so what the zip function does is so all these match up one to one so the first index of neigh is uh matches up with the first index of the heroes list here now if we run zip and it's going to create a list of tuples that match those up for us so it's going to say it's going to be a tuple of Bruce Batman and then the next tooth will be Clark Superman and so on so I'm going to make a dictionary comprehension using this zip function so let me take that out and now you can see that in my comment here I'm saying that I want a dictionary of the name as the key hero as the value for each named hero in zip names and heroes and this is going to be a list of tuples so here in the for loop I am making an empty dictionary and then I'm looping through these tuples and saying that for the name of that tuple I want to be my key in the dictionary and for the hero of that tuple I want to be the value in the dictionary and then I'll print out the dictionary here so if I save that and run it then you can see that we get our dictionary using our for loop so now let's say that we want to do this as a list or as a dictionary comprehension so I'm going to do my dict equal to now instead of the brackets this isn't a listing more it's a dictionary so we're going to use these braces instead so for the dictionary comprehension I'm going to say name and then colon hero for name comma hero in zip Heroes are actually that's going to be names comma Heroes so save that and now let's print this out and you can see that we get the exact same result that we did with our for loop and it's easy to add restrictions onto the ends of these comprehensions so like in this comment here I want to do the same thing but I don't want Peter added to this list so here at the end I can just say if name is not equal to Peter and then run that and you can see that we get our list without Peter and spider-man included in the list so comprehensions really make it easy to add those loops and those conditionals onto the existing comprehension so now let's go ahead and delete the dictionary comprehension example and we'll move on to the set comprehensions so if you don't know what a set is a set is pretty much like a list except it has all unique values so you see here that we have this jumbled mess of a bunch of values between 1 and herbs between 0 and 10 and so whenever we make a set and loop through and add those values to the set the final set is going to have all unique values so if I have this list of nums to do this in a for loop I'm going to say my set equal to an empty set and then for each item in nums my set add that item and now I'm going to print out my set so let's save that and run it and you can see that I get all the unique values from that nums list so now if I wanted to turn this into a set comprehension then I can comment out the for loop there and I'm just going to save my set equal to and this is going to be braces again just like the dictionary comprehension but we're not going to have a colon like the dictionary comprehension head so now we can do this just like the other list comprehension so we want in for in in nums so save that now this is doing exactly what this for loop did it's just adding in for each number in the nums if I print this out and run it then you can see that we get the exact same result that we got with the for loop and just like with the list comprehensions you can add nested loops and conditionals onto the end of this as much as you want so let's go ahead and delete that and so that does it for the comprehensions now I do have this one last example here of generator expressions now generators are a lot different than lists and dictionaries and sets but I wanted to include them in this tutorial because a generator expression is so similar to a list comprehension so if you do want a more in-depth look at the advantages of generators then you can watch my video that I made specifically on generators but in this example I'm just going to focus on generator expressions okay so if we uncomment out this code here this is just a regular generator this isn't an expression so if we look at the comment I'm saying that I want to yield n times n so the square of n for each N and nuns just like our other examples so here our generator function we are passing in this numbness and then we're saying for N and nuns yield the square of n and then here we're saying my gin equals this gin function and we're passing in that list of numbers and then finally here at the bottom we are looping through that generator and printing out each eye so let's save that and run it and you can see that it ran the for loop and printed out all of the values in that generator now if I was to do this with a generator expression the syntax is almost identical to a list comprehension but instead of the brackets or the braces we're going to use parentheses so now just like the other examples we can say I want n times n for n in numbs save that and then we still need to keep our for loop here because we have to iterate through that generator so if I save that and print it out then you can see that we got the exact same result as we did with these lines of code that where we had to create the generator function and do the for loop and then create the generator using the phone and all of that so you can see how this one line of code is not only a lot easier to write and maintain but it's also a lot more readable as well so that about does it for this video on list comprehensions you'll be surprised at how often you'll use these and how much code that you can go through your code base and replace these long nested for-loops with these list comprehensions so if you guys do have any questions just ask in the comment section below be sure to subscribe for future Python videos and thank you guys for watching
Info
Channel: Corey Schafer
Views: 457,660
Rating: undefined out of 5
Keywords: Python (Programming Language), List Comprehensions, Python List Comprehensions, Comprehensions, Python Comprehensions, Set Comprehensions, Dict Comprehensions, Python Tutorials, Python Tutorial, Python, Programming, Programming Tutorials, Computer Science, Software, Software Engineering, Development, Programming Language (Software Genre), How to, How-to, Video Tutorial, Generators, Python Generators, Generator Expressions
Id: 3dt4OGnU5sM
Channel Id: undefined
Length: 18min 29sec (1109 seconds)
Published: Mon Aug 24 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.