Skip to playerSkip to main contentSkip to footer
  • 5/23/2025
List comprehension in Python offers a concise way to create lists based on existing iterable. It provides a more readable and often more efficient alternative to traditional for loops when constructing lists.
Transcript
00:00List Comprehension
00:02List Comprehension
00:04List Comprehension
00:06List Comprehension
00:07Concept
00:08First
00:09I am going to get a exercise
00:10So you will know
00:12Where are we going
00:13Where are we going
00:15Exercise
00:16You will create a list
00:18Using for loop
00:201 to 10
00:22Very simple
00:24List 1 is equal to
00:27Empty list
00:28For i in
00:30Range
00:3110
00:32And then
00:33You can do this
00:35Li-1
00:37Append
00:38Append
00:39Append
00:40Append
00:41Append
00:42Print
00:43Li-1
00:45List
00:46List
00:47Print
00:481 to 10
00:49You can do this
00:51You can do this
00:52You can do this
00:54Very good
00:56Excellent
00:57Now
00:59Question
01:00My question
01:01Is
01:02Which way
01:03We can do this
01:04Compact
01:05Compact
01:06Comprehension
01:07That means
01:08You can do this
01:09In English
01:10Literature
01:11You can do this
01:12Comprehensive paragraph
01:13Comprehensive
01:14Comprehensive
01:15Comprehensive
01:16Comprehensive
01:17Comprehensive
01:18Comprehension
01:19Basically
01:21What you do
01:22Is
01:23That
01:24We have to do
01:25Single statement
01:26And
01:27How is
01:28The
01:29Keep
01:30Self
01:31Comprehensive
01:32I have to
01:33Do
01:34For
01:35To
01:36This is what we have, this expression is not a variable, this is a variable, this is a expression,
01:45this is a expression, so this is a expression.
01:49So what do we need to do? List in the expression.
01:54List in the expression.
01:56What do we need to do?
01:58Then we need to do for loop.
02:01I remove this for loop.
02:05For loop is not a problem.
02:10That's it guys.
02:12Now print li1.
02:14Li underscore one.
02:17Execute.
02:18Kaboom shakala.
02:20Same result.
02:21Over to know.
02:22Maza aya ki nahi?
02:23A single line ke anndar hemne kam kar diya.
02:26This is case one.
02:28We use the expression to a variable.
02:32I'm going to divide it.
02:33I'm going to divide it.
02:34How many parts are?
02:35Basically.
02:36In a list comprehension case one, there are two parts.
02:39Here I'm going to divide it.
02:41This part is basically loop.
02:45However, this part is the expression.
02:47By the way, this part is the expression.
02:59Expression.
03:00Loop.
03:01Expression.
03:02Those parts are the two parts, expression and loop.
03:04Okay, this was the first case.
03:08In this case, we have to use the expression go single as a variable.
03:11Now, I have a question for you.
03:14You can create a list of 0 to 9.
03:18You can create a list which is 0 to 9 square.
03:23This is very cool.
03:25You can say, sir, this is my hand-hand.
03:27You have to ask questions.
03:29You can give it a try.
03:33Otherwise, follow me.
03:35List 2.
03:35You can create a list.
03:38For I in range.
03:41Range is 10.
03:43Then, you can create a list of li1.
03:46You can create a list of li2.
03:49You can create a list of li2.
03:50And the i is square.
03:54And then print it.
03:56You can create li2.
03:57Li underscore 2.
04:01You can execute a list of li2.
04:03You can use a list of li3.
04:04This is the very cool.
04:07This is the very cool.
04:08Very good.
04:09This is the case 2.
04:10If I say that if you want to list comprehension, how do you do it?
04:16Now look at this loop, I will break it in list comprehension.
04:19List comprehension, which list has created.
04:22This list has created.
04:23Empty created.
04:24This is the expression.
04:27You can put it here.
04:30This is what we have done with variable mic.
04:35Next is the loop.
04:37You can put it here.
04:39This is the redundant code.
04:44This is the same.
04:46This is the same.
04:50This is the same.
04:52This is the same.
04:54This is the same.
04:56This is the same.
04:58Very easy.
04:59Now I will iterate on list comprehension.
05:02This list has two parts.
05:05This is the same.
05:07This is the same.
05:08The next one is the expression.
05:10This is the loop.
05:12I want to say that I will say loop.
05:16This is the expression.
05:18This is the expression.
05:20This can be any same.
05:22You should note that this is a full flag expression.
05:27Here I am just multiple by two.
05:29This is two parts.
05:31At the moment, we have two parts.
05:33Good.
05:35The first case is just variable.
05:38The second one is just expression.
05:41Now, the third question is about you.
05:42Ready?
05:44Now, I will answer one Pepsi bottle.
05:49The question is this.
05:51Now, if you want to generate this list,
05:55you can select even.
05:58You can filter me.
06:00I will use the filter function.
06:03But if you want to use the filter function,
06:05you can select if you can.
06:07You can select even number.
06:10Let's start with Pepsi bottle.
06:13Otherwise, let's get started.
06:15Let's create a list list 3.
06:18This is empty list.
06:19So, very good.
06:22Now, what do we do?
06:24We have basically, we have to do this.
06:27We have to do this.
06:29We have to do this.
06:30We have to filter out.
06:32Okay.
06:33Now, let's do this.
06:35Okay.
06:54sqr i sqr i sqr
06:58okay
07:00I
07:02I
07:04I
07:06I
07:08I
07:10I
07:12I
07:14I
07:16I
07:18I
07:20I
07:22I
07:23I
07:24ah
07:38I
07:40I
07:42I
07:44I
07:46I
07:47I
07:4936, 64.
07:50You have a list.
07:52Which is?
07:53Even number.
07:54Very good.
07:57Now, what are the code?
08:001 loop, 1 conditional, 1 operation.
08:043.
08:05This is case 3.
08:07Now, this is the list comprehension.
08:10Now, we will have a list comprehension.
08:12Magic.
08:13This is a Pepsi bottle.
08:15You have to have a Pepsi.
08:17I will have a Pepsi.
08:20Now, I will have this list.
08:22I will have this list.
08:24I will just go ahead and get to it.
08:26You can take a machine.
08:27You can also take a machine to add.
08:29So, you can turn the sonraki on the other end.
08:31This is a list.
08:33I have this list to create.
08:35Next is the expression.
08:38If I have an expression, I will remove you.
08:40I will remove it.
08:42Now, this expression.
08:44Now, this expression,
08:46I told you that the for loop is a good thing.
08:48Yes, it's a good thing.
08:50For loop.
08:52For loop on the for loop.
08:54For loop on the for loop.
08:56If we have a for loop on the for loop,
08:58we can execute it and then execute it.
09:00Then perform it.
09:02For loop on the for loop.
09:04For loop on the for loop.
09:06This is not a good thing.
09:08I will do it.
09:10Here you have.
09:12This was original code.
09:14This is a list comprehension code.
09:16Very very simple.
09:18Execute and the result will be exactly the same.
09:22This is a list comprehension result.
09:24This is a normal result.
09:26Right?
09:28I will break down.
09:30A list comprehension of three parts.
09:34I will divide them.
09:36First, second and third.
09:38First, second and third.
09:40First, second and third.
09:42First, first.
09:44The first is the expression.
09:46The second is the loop.
09:48The third is the condition.
09:50Now, you'll use the condition.
09:52The condition will not be done.
09:54The condition will not be done.
09:56The condition will not be done.
09:58However, the expression or loop are a list comprehension.
10:02However, expression and loop
10:04are a list comprehension
10:06of a list comprehension.
10:08This expression and loop
10:11can be a list comprehension.
10:14This is clear.
10:16Very good.
10:18This is a list comprehension.
10:22This is clear.

Recommended