17 - how do I use list comprehensions in python (Python tutorial for beginners 2019)
Comprehension in Python: A Powerful Tool for Working with Lists and Dictionaries
One of the most powerful features of Python is its comprehension syntax, which allows developers to create new lists and dictionaries by applying operations to existing ones. In this article, we'll explore the different types of comprehensions available in Python and how they can be used to simplify code and improve productivity.
Let's start with a simple example. Suppose we have a list of names and we want to add "lastly" to the end of every name. We can use a for loop to achieve this, but it's not the most elegant solution. Instead, we can use a comprehension to create a new list with the modified names. The syntax is straightforward: `names = [name + ' lastly' for name in names]`. This will create a new list where each element is the original name followed by "lastly".
Another common use of comprehensions is when working with lists and dictionaries. Suppose we have a list of movies and their corresponding ratings, and we want to filter out the ones that don't meet a certain threshold. We can use a comprehension to create a new list that only includes the movies with ratings above 6. The syntax is `movies = [movie for movie in movies if ratings[movie] > 6]`. This will create a new list where each element is a movie name, but only if its rating is greater than 6.
Comprehensions can also be used to nest loops and conditional statements. For example, suppose we have a dictionary of movies with their corresponding ratings, and we want to filter out the ones that don't meet a certain threshold. We can use a comprehension with nested loops and an `if` statement to achieve this: `[movie for movie in movies if movies[movie] > 6]`. This will create a new list where each element is a movie name, but only if its rating is greater than 6.
One of the most powerful features of comprehensions is their ability to be nested. This allows us to perform complex operations on lists and dictionaries that would otherwise require multiple loops or conditional statements. For example, suppose we have a dictionary of movies with their corresponding ratings, and we want to filter out the ones that don't meet a certain threshold. We can use a comprehension with nested loops and an `if` statement to achieve this: `[movie for movie in movies if movies[movie] > 6 for rating in ratings if ratings[rating] > 5]`. This will create a new list where each element is a movie name, but only if its rating is greater than both 6 and 5.
Another useful feature of comprehensions is their ability to be used with dictionaries. Suppose we have a dictionary of movies with their corresponding ratings, and we want to filter out the ones that don't meet a certain threshold. We can use a comprehension with an `if` statement to achieve this: `[movie for movie in movies if movies[movie] > 6]`. This will create a new list where each element is a movie name, but only if its rating is greater than 6.
In addition to their ability to simplify code and improve productivity, comprehensions also have several other benefits. They can be used to reduce the amount of memory required by a program, since they don't require the creation of intermediate lists or dictionaries. They can also be used to make programs more concise and easier to read, since they allow developers to express complex operations in a single line of code.
Conclusion
In conclusion, comprehensions are a powerful tool for working with lists and dictionaries in Python. They allow developers to simplify code and improve productivity by enabling the creation of new lists and dictionaries with a single operation. They can also be used to reduce memory requirements, make programs more concise, and improve readability. Whether you're working on a small project or a large-scale application, comprehensions are definitely worth considering.
"WEBVTTKind: captionsLanguage: enhey guys what's up how's it going this is Kazi from claro programmer now in this tutorial was that better yeah I want you to take his seat oh can we just edit it like oh yeah oh yeah yeah yeah just and I'm doing like fault all right now you go sit this is a rolling yeah I'm taking over boys let's do this sup you guys this is Eric and clever programmer and we just have a quick short little video for you today all we're gonna be talking about list comprehensions in Python so this is a neat little trick that uh that is unique the Python I believe I don't know any other programming languages that have it but pretty much what it allows you to do is to make lists out of cool little expressions so you can actually populate an entire list with just one line with this little um unique thing a little bit weird to explain but I'll just show you and then you see how it works so let's get started let's say we have a list called names let's just put in some names here so we have Jennifer Susan Jane and Sophie alright cool so this is our short little list list of names and um first of all if you wanted to put all of these names in a list then the first way you can do it was actually use a for loop so I will show that first this is the way that you guys should be familiar with doing so we would just have an empty list here and then we would have to iterate for every person in the list we would simply append that person to the new list so what we did here was we put every name in the names list and put it into the L list so we're just gonna print this out just like that give this a run and um there's an error oh let's see what's going on here oh yeah there we go stupid me you guys pardon me we want to append the person to the list not the list to the list all right so let's run that again and as you can see the UM all the names are printed out nice and nice and clean here so what we did here was we created a new list we read from the list and we put everything um in that list into a new list and printed that new list pretty cool right now I'm gonna show you the list comprehension for this so the way you can um the way you do that is you would actually just make a list like this so the syntax for this is the item that you want to be in the list goes first so we could just put a person here on I'll show how this relates to the for-loop later but we'll go here and then we'll say for person in names simply like that so what's happening here is we actually have a for loop in in one line here so it's gonna loop through so person is gonna loop through this for loop is gonna loop through names and each element in the names list will be person and all we're doing is simply taking this variable here and we're sticking it here and these things that are over here will be what's actually in the list when we're done so let's try to print this and see what happens it should be exactly the same go like that hit run and as you can see the output is the same both times so these two pieces of code this for loop and this list comprehension are actually equivalent but you see how we collapse these four four lines of code into one line that's the power of list comprehensions so that's the most basic one that everybody learns first that's the one I learned first but actually the true power of list comprehensions is that if you have a list of something you can actually do a Shinzon every element of the list and create a new list that way so let's say you had a bunch of numbers in a list you had one two three four and then you wanted to maybe multiply everything by two so it'd become two four six eight just like that you could use the list comprehension to do an operation on every element and it would come out like that so let's how let's have another example um let's make another list and I'm gonna change I'm actually into an operation on all of these so let's create another empty list here this is gonna overwrite the empty list cell from above actually let's just paste this in boom just like that so for person in names we can actually um edit everything in here so if we instead of just wanting to add person we could actually edit this thing before we add to list so that it looks new so all I'm going to do here is a very simple and um slightly personal thing but I'm just gonna type in dumped me right here so now let's run this and as you can see this has suddenly turned into a list of X's not just people alright so let's go on turning of this tool this comprehension will be as simple as copy and pasting this and the way this will work is for each person in in the names list we would actually just add the string to here just like that what am i doing you guys so as you can see everything is reiterated here I probably written this out on paper many many times but it's actually on a stream now forever in the electronic abyss let's move on so that's how all this comprehension works all you do is you can actually go through an entire list so the list we're going through here is names and we wanted to do an operation to every element in that list which is ADD dumped me to the end of every name and so I actually just put those things together and put it in this new list so now the list looks like that you guys falling lastly there's actually a couple other cool things you can do you can actually get pretty verbose in these and do some weird nesting but then it gets a little bit messy it might actually be better just use for loops at that point like nested for loops but if it's just two statements that might actually be okay like two nested for loops are a for loop with an if statement inside we'll try that next so getting to a slightly more positive note we'll go back to on this list l Oh actually yeah well we'll have a new list down here let's just start something new so let's have a dictionary called movies and ratings just like that alright and then we're just going to have interstellar like that and give this a rating of nine out of ten and then we'll also have dark night let's move this over so you can see dark night rating of eight we'll have fifty shades reading of three and then we'll have again fifty shades darker rating of two and this has this progressing you can see how this is going 50 shades darkest reading of one okay so now let's say we have we have this dictionary here yeah you can also run uhm comprehensions over dictionary so this will actually call the dictionary comprehension you can make a list here and let's say I just wanted to filter out all of the movies that have a rating less than six so I only want to show the ones that have a reading more than more than six so how that would work would just be four movie in movies and ratings what they're autocomplete that they're if movies and ratings at movie is greater than 6 so we're filtering out here so if the movie is greater than six then we will append it to the the anti empty new list so that will just be movie just like that and then we'll print this out and let's see how that works these three movies should have been cut out and then it should only show these two here I should just show the names because when we index into a dictionary only they'll key here the key string here is is put in the list let's run this and as you can see ignore all this stuff I'm over it but we can see that the the three movies of that and anything with the rating less than six and on these fifty shades ones all got cut out and interstellar in the dark night oh these awesome sci-fi and superhero movies stayed in so that's how that works but we can actually put this all into a list comprehension this will be the final one because it's a little bit complicated but if you understand this you can pretty much understand any list comprehension because you can do I don't think there's a limit to it but you could probably nest as many as you want into one line but it gets unruly at some point so just be conscious of that but this is great for saving a lot of space for a short little for the oops so this one falls on you guys is going to be movie so we want the movie name to be in the list for movie in movies in the list movie in ratings but we only want the movies that are in movies and ratings if the movies and movies and ratings at movie is greater than 6 so what this is saying is for every movie movie about ratings or a movie and readings if if the movie rating is greater than 6 then stick the movie name in the list so let's see how that runs it should have the same exact output and as you can see has the same exact output is a for-loop all done in one line so let's take a one final look at this to compare the code so this first example we have a list of names and then this for-loop became this last comprehension this for-loop became this list comprehension and this for-loop became this list comprehension um that's pretty much it you guys i'm really cool little thing you can do in Python a very good for being very descripted in a single line of code and super super useful if you want to do a some kind of calculation on every element of a list and create a new list from that it's probably one of my favorite things in Python I don't know about you guys but yeah just let me know what you guys think in the comments um and that should be it for this video thanks you guys for watching and see you later good byehey guys what's up how's it going this is Kazi from claro programmer now in this tutorial was that better yeah I want you to take his seat oh can we just edit it like oh yeah oh yeah yeah yeah just and I'm doing like fault all right now you go sit this is a rolling yeah I'm taking over boys let's do this sup you guys this is Eric and clever programmer and we just have a quick short little video for you today all we're gonna be talking about list comprehensions in Python so this is a neat little trick that uh that is unique the Python I believe I don't know any other programming languages that have it but pretty much what it allows you to do is to make lists out of cool little expressions so you can actually populate an entire list with just one line with this little um unique thing a little bit weird to explain but I'll just show you and then you see how it works so let's get started let's say we have a list called names let's just put in some names here so we have Jennifer Susan Jane and Sophie alright cool so this is our short little list list of names and um first of all if you wanted to put all of these names in a list then the first way you can do it was actually use a for loop so I will show that first this is the way that you guys should be familiar with doing so we would just have an empty list here and then we would have to iterate for every person in the list we would simply append that person to the new list so what we did here was we put every name in the names list and put it into the L list so we're just gonna print this out just like that give this a run and um there's an error oh let's see what's going on here oh yeah there we go stupid me you guys pardon me we want to append the person to the list not the list to the list all right so let's run that again and as you can see the UM all the names are printed out nice and nice and clean here so what we did here was we created a new list we read from the list and we put everything um in that list into a new list and printed that new list pretty cool right now I'm gonna show you the list comprehension for this so the way you can um the way you do that is you would actually just make a list like this so the syntax for this is the item that you want to be in the list goes first so we could just put a person here on I'll show how this relates to the for-loop later but we'll go here and then we'll say for person in names simply like that so what's happening here is we actually have a for loop in in one line here so it's gonna loop through so person is gonna loop through this for loop is gonna loop through names and each element in the names list will be person and all we're doing is simply taking this variable here and we're sticking it here and these things that are over here will be what's actually in the list when we're done so let's try to print this and see what happens it should be exactly the same go like that hit run and as you can see the output is the same both times so these two pieces of code this for loop and this list comprehension are actually equivalent but you see how we collapse these four four lines of code into one line that's the power of list comprehensions so that's the most basic one that everybody learns first that's the one I learned first but actually the true power of list comprehensions is that if you have a list of something you can actually do a Shinzon every element of the list and create a new list that way so let's say you had a bunch of numbers in a list you had one two three four and then you wanted to maybe multiply everything by two so it'd become two four six eight just like that you could use the list comprehension to do an operation on every element and it would come out like that so let's how let's have another example um let's make another list and I'm gonna change I'm actually into an operation on all of these so let's create another empty list here this is gonna overwrite the empty list cell from above actually let's just paste this in boom just like that so for person in names we can actually um edit everything in here so if we instead of just wanting to add person we could actually edit this thing before we add to list so that it looks new so all I'm going to do here is a very simple and um slightly personal thing but I'm just gonna type in dumped me right here so now let's run this and as you can see this has suddenly turned into a list of X's not just people alright so let's go on turning of this tool this comprehension will be as simple as copy and pasting this and the way this will work is for each person in in the names list we would actually just add the string to here just like that what am i doing you guys so as you can see everything is reiterated here I probably written this out on paper many many times but it's actually on a stream now forever in the electronic abyss let's move on so that's how all this comprehension works all you do is you can actually go through an entire list so the list we're going through here is names and we wanted to do an operation to every element in that list which is ADD dumped me to the end of every name and so I actually just put those things together and put it in this new list so now the list looks like that you guys falling lastly there's actually a couple other cool things you can do you can actually get pretty verbose in these and do some weird nesting but then it gets a little bit messy it might actually be better just use for loops at that point like nested for loops but if it's just two statements that might actually be okay like two nested for loops are a for loop with an if statement inside we'll try that next so getting to a slightly more positive note we'll go back to on this list l Oh actually yeah well we'll have a new list down here let's just start something new so let's have a dictionary called movies and ratings just like that alright and then we're just going to have interstellar like that and give this a rating of nine out of ten and then we'll also have dark night let's move this over so you can see dark night rating of eight we'll have fifty shades reading of three and then we'll have again fifty shades darker rating of two and this has this progressing you can see how this is going 50 shades darkest reading of one okay so now let's say we have we have this dictionary here yeah you can also run uhm comprehensions over dictionary so this will actually call the dictionary comprehension you can make a list here and let's say I just wanted to filter out all of the movies that have a rating less than six so I only want to show the ones that have a reading more than more than six so how that would work would just be four movie in movies and ratings what they're autocomplete that they're if movies and ratings at movie is greater than 6 so we're filtering out here so if the movie is greater than six then we will append it to the the anti empty new list so that will just be movie just like that and then we'll print this out and let's see how that works these three movies should have been cut out and then it should only show these two here I should just show the names because when we index into a dictionary only they'll key here the key string here is is put in the list let's run this and as you can see ignore all this stuff I'm over it but we can see that the the three movies of that and anything with the rating less than six and on these fifty shades ones all got cut out and interstellar in the dark night oh these awesome sci-fi and superhero movies stayed in so that's how that works but we can actually put this all into a list comprehension this will be the final one because it's a little bit complicated but if you understand this you can pretty much understand any list comprehension because you can do I don't think there's a limit to it but you could probably nest as many as you want into one line but it gets unruly at some point so just be conscious of that but this is great for saving a lot of space for a short little for the oops so this one falls on you guys is going to be movie so we want the movie name to be in the list for movie in movies in the list movie in ratings but we only want the movies that are in movies and ratings if the movies and movies and ratings at movie is greater than 6 so what this is saying is for every movie movie about ratings or a movie and readings if if the movie rating is greater than 6 then stick the movie name in the list so let's see how that runs it should have the same exact output and as you can see has the same exact output is a for-loop all done in one line so let's take a one final look at this to compare the code so this first example we have a list of names and then this for-loop became this last comprehension this for-loop became this list comprehension and this for-loop became this list comprehension um that's pretty much it you guys i'm really cool little thing you can do in Python a very good for being very descripted in a single line of code and super super useful if you want to do a some kind of calculation on every element of a list and create a new list from that it's probably one of my favorite things in Python I don't know about you guys but yeah just let me know what you guys think in the comments um and that should be it for this video thanks you guys for watching and see you later good bye\n"