Now let’s understand list comprehensions this is a slightly more advanced concept compared to list a list comprehension.
Now, remember earlier, I was giving you one to one problem and in order to solve that problem you need to understand list and list comprehension. We have now understood what list is let’s get to a list comprehension.
Now, let’s talk comprehension is a way of creating a list based on various different logics. Now here see this M’s values this M contains this particular array or a list of lists.
From this list comprehension what it is basically is a way where you can create a list out of this based on whatever rules that you want to create for example, here you are creating a list of all the first items, the output here is incorrect. So, if I run this, it will give you for every row in M.
So this will continue it is referring to by row I mean so 123456789 This is for row row zero, this is row 102012. So for every for row in M for iterating. This basically you started iteration now. So every row in M first in that row, so the row object in the very first iteration will contain the values one comma two comma three, it will contain this list.
From this it is taking this position, zero position. In iteration one, it will pick one. Alright, in the next iteration, this guy gets picked up from this list, you pick four, because we are asking for the zero position, right? So you pick four, then again, the next iteration, you pick the seventh position, the 700, which is seven. So this form is the output.
All right, Now let’s look at one more problem. One more case, earlier, we saw that we needed to multiply say we have this list 123456789.
All right, now I am taking let’s take a simpler case, let’s take 12345. This particular list, every item of this list, we wanted to multiply by three. But by doing this, the same list is getting appended three times. And that is going to be a final list. Internally, it will be 12345 repeated three times. This was the output of whatever we want to multiply this, multiply each of these elements by three, you can use list comprehension here, right? Let’s do that. Let’s try and do that.
So I’m declaring this L equals two formulas one comma, two comma three comma four comma five. So this is L, what would be the syntax looking like in order to multiply each of these elements by the value three. So start writing the list comprehension, every list comprehension will have an outer back at the outset, there will be a bracket.
Alright, so to start writing the rest of list comprehension, you start with the for statement for item L in list.
Typically, every list comprehension, when you start writing it this step, this is where you start. Then now we have caught i for every item in the list, you want to do something with that either you can suppose in the previous case, it was a row here, right now I’m using, it doesn’t matter what variable name you use here, as long as it’s some valid variable, valid character.
It could be j, it could be anything in anything out there. Alright. So for i in L. By doing this, it will start iterating one element after the other.
Now we multiply i with the value three. So now when you’re talking about i in L, you’re talking about an integer one, then integer two, and three, and so forth, so on and so forth.
So one you multiply by three means it will multiply the number one with three will give you the value three. Likewise, you multiply the number two with three will give you six. It won’t repeat two. Yeah, it won’t repeat two. So it will give you the actual result.
Now, let’s take this case, what would happen instead of I am using STR of five? What would be the output of this? Can you imagine what would happen? Can you guess, you convert it to a string by doing this, you are converting it to a string and then multiplying it with three.
Now what would be the behavior if you are multiplying a string with three, it will get repeated. So the string one if you’re multiplying it with three, it will get repeated this many number of times. hitting me. So to create such type of custom logic, you can use list comprehension, very powerful, right?
So basically, where you want to use list comprehensions you want to do something with every item of this list. If you want to do something with every item of this list, whatever be the logic, you want to iterate on. into some sort of a multiplicity of the item in that list you can use list comprehension like how we are doing it here alright now let’s look at how you can use list comprehension in a different area altogether.