List Comprehension

A free video tutorial from Next Edge Coding
Full Stack Developer & Data Enthusiast
Rating: 4.2 out of 5Instructor rating
3 courses
21,892 students
List Comprehension

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 and do so in the last video, I have talked about how you can create glasses and also objects of those glasses and 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? Will list comprehensions are 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. Yeah, numbers. And I will store here numbers from one to 10. Right. So this is my list, right? So I will just executed an individual explorer. As you can see, I have a list of numbers now. See, 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 look to this list, right? We can look 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. Call new numbers. First of all, it will be an empty list and then we will look through this list. Right. So I would like to have a number in numbers. We can write a new number, not pen and appendix number over here. Right. So if I execute it, we got a new numbers list. Right. It also contains numbers from one to ten. But if you use a 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 a 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 four 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. 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 four, 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 four number in numbers. And now we need to check whether the number is less than five or not. So we would like it if number is less than equals five. And if I run it, well, check this out. Now, this list contains numbers from one to five, and that's because of this simple condition over here. So whenever a number is greater than five, we are not putting that number inside of this new numbers list. And now let's take 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 or equal numbers to and here I will store one three five seven nine. Now, you know, I am going to store it now. First of all, what I tell you what I'm trying to do over here. So I want to select all the numbers from this May 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 our new numbers equals in a list. Pretty much do the same thing. Number four, 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 right here. If number not in numbers to. Right. So let me execute it. Well, obviously, it's not defined because we need to run it first, so if I run it now, check this out. We have all the different even numbers over here. So we have two, four, six, eight and 10. 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 list. 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 her number multiplied by two for number in numbers. And if I execute it, look what I got. Two, four, six, eight, 10, 12, 14, 16, 18 and 20. Right. So similarly, I can also do one more thing over here. Like new numbers equals. Number four, number in numbers, if number modulars two 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 right here the generator comprehension. And I would like to square again equals now instead of writing this square brackets over here. I will write this ellipses. And inside of this I will write no double asterisks to you know, this pretty much means power, right? So no square. So this means no square and phone 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 straight 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 lowest rate. 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 generally, you know, the dictionary comprehensions so we can even create dictionaries like this. So. Shall we have a dictionary, you know, like Apple? Then orange. We have well, you know, 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 and apples that we have. So let me run it so that this is our dictionary. Right. Apple one. Banana 10. 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, another thing that we did over here. Right. Number four, 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 on my date of key for key in mind, that case. Now, if you remember Maidique, that gives 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 addictive key. Right. So key is the key that we fetch from my keys. And my degree 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 and this is new data and they are pretty much identical. And now, see, we want to create another dictionary with all those fruits, you know, who's number or whose count is greater than five so we can create it like this, we will right hand Newdegate. And it will be pretty much the same mighty turkey for Key in my day doggie's, and then we need to check if my date 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 invite and you know, you do the list comprehension or you can also get a 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 statement and I then the next number is love and then it contains Avenger's. So say you have this list and what do you want to do is you want to join this list. So, you know, we have the separate words over here, right? We have I then we have love, then we have Avenger's. And you want to join all of these words to form a sentence, which is I love Avenger's. Now, if you want to do it, normally, what you can do is you can declare the empty string over here and you can look through this whole list and then append each day. And 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 would like to sentence that's going to contain the whole sentence after joining. And here I will write a space and then space dodgin and I will pass here the whole list. So let me run it. And we should have ascendence over here and we have to check this out, this sentence as I love Avenger's, so what really happened over here? So, you know, this piece was a delimiter. And what we did is we called the joint on this delimiter and we passed the last words. So what it did, disjoined function did was it added space between all of these different words and it returned a whole string and that string was 11 injuries. So, you know, instead of space, if we pass here something else, like a full stop and we write joint and we pass the words and if I run it. Well, check this out now, the sentences I don't love not having injuries, 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 our 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 joint function. Right. And that's it. And I will see you in the next one.