List Comprehension
A free video tutorial from Next Edge Coding
Full Stack Developer & Data Enthusiast
3 courses
26,713 students
Learn more from the full course
Hands On Natural Language Processing (NLP) using Python
Learn Natural Language Processing ( NLP ) & Text Mining by creating text classifier, article summarizer, and many more.
10:28:36 of on-demand video • Updated September 2019
Understand the various concepts of natural language processing along with their implementation
Build natural language processing based applications
Learn about the different modules available in Python for NLP
Create personal spam filter or sentiment predictor
Create personal text summarizer
English [Auto]
Hello and welcome to this Python tutorial. So in the last video I have talked about how you can create classes and also objects of those classes and call different functions inside objects and so on. So in this video I will show you how you can perform list comprehension in Python. So what is list comprehension in the first place? Well, list Comprehensions are, you know, simpler and intuitive ways by which you can create a list in Python. So let me give you an example. So I will declare a list over here called numbers. Well, yeah, numbers. And I will store here numbers from 1 to 10. Right. So this is my list, right? So I will just execute it. And in the variable Explorer, as you can see, I have a list of numbers. Now, say I have to create a copy of this list, right? So I have to create another list that contains the same numbers. So how can we do that? Well, we can loop through this list, right? We can loop through this list and append each and every item to the new list as well. So let's do it. So we will have another list over here called New Numbers. First of all, it will be an empty list and then we will loop through this list, right? So I will write here for number in numbers. We can write new number dot append, and I will append this number over here. Right? So if I execute it, we got a new numbers list, right? It also contains numbers from 1 to 10, but. If you use list comprehension, then you can do pretty much all these three lines of code in a single line. So let me show you how you can do that using list comprehension. So this is a normal way, right? And if you use list comprehension. So let me just initialize this new numbers again to an empty list. So now I will write new numbers equals those square brackets because it's a list. And then I will write here number for number in numbers and that's it. Let me run it. Well, we have the new list. Just check this out. You know, this is three lines of code and this is just a single line of code. So what really happened over here? So in this way, we are pretty much defining what will be each of the numbers that will be stored in this new numbers list. Right. So for each of the numbers, we are pretty much going to have this. So here number, right? Number is going to be the element that will be stored in this new numbers list. So what is this number? This number is for number in numbers, right? So we are pretty much going to take each of the numbers from this numbers list and we will store it inside this new numbers list. And that's what we are doing over here, number for number in numbers. So let's do a few more operations. So say we want to store only those numbers that are less than or equal to five. Right. So we can write here new numbers equals number for number in numbers. And now we need to check whether the number is less than or equal to five or not. So we will write here if number is less than equals. Five. And if I run it, we'll check this out. Now, this list contains numbers from 1 to 5, and that's because of this simple condition over here. So whenever number is greater than five, we are not putting that number inside of this new numbers list. And now let's check that how we can create a new list of numbers, you know, based on the numbers in two different lists. So I will create one more list over here called Numbers two. And here I will store one, three, five, seven nine. Now, you know, I am going to store, first of all, what I will just tell you what I'm trying to do over here. So I want to select all the numbers from this main numbers list that are not contained in this numbers to list. Right? So I will just select all of those numbers and put them inside this new numbers list. So I will write here new numbers equals in a list will pretty much do the same thing. Number for number in numbers. And you know, if I just leave it over here, then I will just select all the numbers from this numbers list. But I also need to make sure that this number is not contained in this numbers to list. So I can do it by writing here if number not in numbers to. Right. So let me execute it. Well, yeah, obviously it's not defined because we need to run it first. So if I run it now, so we'll check this out. We have all the different even numbers over here. So we have two, four, six, eight and ten and we have excluded the numbers, right? One, three, five, seven and nine. So this is one more way by which you can create a new list based on the number stored in one or more lists. Now, let's do just, you know, a few more operations over here to get things clear. So say I want to create a new list by multiplying two with each of the numbers stored in this numbers list. So I can write here number multiplied by two for number in numbers. And if I execute it, look what I got. Two, four, six, eight, ten, 12, 14, 16, 18 and 20. Right? So similarly, I can also do one more thing over here. Like new numbers equals. Number for number in numbers. If number modulus two is equals equals one. So in this way I am only selecting the odd numbers out of this numbers list. So if I execute it I have one three, five, seven nine and I have excluded all the even numbers from the list. So these are a few operations that you can perform. And, and now let's look at some of the generator Comprehensions. So I will let you know what that is. So let's write here generator comprehension and I will write here square n equals. Now, instead of writing this square brackets over here, I will write this ellipsis and inside of this I will write number double asterisks to, you know, this pretty much means power, right? So number square. So this means number square and for number in numbers. Right. And it should be in. So let me execute it. So this is a generator, right? This over here is a generator. This is not a list. Now, to get the list out of it, we need, we can just pass it inside of a list. So we got the list right. We got the list of all the numbers squared one, four, nine, 16, and so on. So this is how you can even store the different generators like this. And, you know, later on you can just pass that generator into a list to generate the list, right? So we are pretty much done with list comprehension. Now, before we end this video, let's try out a few more different types of comprehensions which are, you know, the dictionary comprehensions So we can even create dictionaries like this. So. Say we have a dictionary. You know, like Apple. Then orange. We have? Well, yeah, right over here. We have banana. So this is pretty much a simple dictionary. And, you know, it contains Apple, orange and banana as its keys. And these numbers are the, you know, the number of bananas or apples that we have. So let me run it. So this is our dictionary, right? Apple one, banana, ten, Orange four. So what I want to do now is say I want to create a new dictionary. And that dictionary would be same as this dictionary, right? So similarly, you know, the thing that we did over here, right? Number for number in numbers, we copied this whole numbers list into this new numbers list. So we are going to do the same for this dictionary. So let's find out how we can do that. So instead of square brackets, we have again here the curly brackets. And inside of this we are going to write key colon my dict of key. For key in my dict dot keys. Now if you remember my keys returns the list of keys. Right. So we are checking that for each key in the list of keys of my dict. We are pretty much storing this dictionary. Right. So we are pretty much storing here key and my dict of key. Right. So key is the key that we fetch from my dict of keys and my dict of key is the value of that key. Right. So if we run this we should have another dictionary that's pretty much the same as you can see over here. Right. So this is my dict and this is new dict and they are pretty much identical. And now say we want to create another dictionary with all those fruits, you know, whose number or whose count is greater than five. So we can create it like this. We will write a new dict. And it will be pretty much the same. My dict of key for key in my dict keys. And then we need to check if my dict of key is greater than five. So if I run it, I should only have banana. And I have banana. Right? Because it's the only fruit with count greater than five. And that's it. So this is how you do list Comprehensions in Python, you know, you do the list comprehension or you can also do the dictionary comprehension. And now before I end this video, there is just one last method that is really important for you. So say you have this list, you know this contains. I love our state, contends I. Then the next number is love. And then it contains Avengers. So say you have this list and what you want to do is you want to join this list. So, you know, we have this separate words over here, right? We have I then we have love, then we have Avengers. And you want to join all of these words to form a sentence, which is I love Avengers. Now, if you want to do it normally what you can do is you can, you know, declare an empty string over here and you can loop through this whole list and then append each time, right? Each time you will add the word or the item that you loop through into the string, right? So you will get the whole string. But we have a much more efficient way to do that. So I will write a sentence that's going to contain the whole sentence after joining. And here I will write a space and then space dot join and I will pass here the whole list. So let me run it and we should have a sentence over here and we have it. Check this out. This sentence has I love Avengers. So what really happened over here? So, you know, this space was a delimiter and what we did is we called the join on this delimiter and we passed the list words. So what it did this join function did was it added space between all of these different words and it returned a whole string. And that string was I love Avengers. So you know instead of space, if we pass here something else like a full stop and we write, join and we pass the words and if I run it. Well, check this out. Now, this sentence is I love Avengers, right? So this is pretty much how you can join a whole list to form a single sentence. And we are going to need this thing later on at a project. So that's pretty much it. In this video, you have learned a lot about list comprehension, dictionary comprehension, and also about this intuitive join function. Right? And that's it. And I will see you in the next one.