Skip to player
Skip to main content
Skip to footer
Search
Connect
Watch fullscreen
Like
Comments
Bookmark
Share
Add to Playlist
Report
Lecture 78: List Comprehension in Python
Nafees AI Lab
Follow
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.
Category
π
Learning
Transcript
Display full video transcript
00:00
List Comprehension
00:02
List Comprehension
00:04
List Comprehension
00:06
List Comprehension
00:07
Concept
00:08
First
00:09
I am going to get a exercise
00:10
So you will know
00:12
Where are we going
00:13
Where are we going
00:15
Exercise
00:16
You will create a list
00:18
Using for loop
00:20
1 to 10
00:22
Very simple
00:24
List 1 is equal to
00:27
Empty list
00:28
For i in
00:30
Range
00:31
10
00:32
And then
00:33
You can do this
00:35
Li-1
00:37
Append
00:38
Append
00:39
Append
00:40
Append
00:41
Append
00:42
Print
00:43
Li-1
00:45
List
00:46
List
00:47
Print
00:48
1 to 10
00:49
You can do this
00:51
You can do this
00:52
You can do this
00:54
Very good
00:56
Excellent
00:57
Now
00:59
Question
01:00
My question
01:01
Is
01:02
Which way
01:03
We can do this
01:04
Compact
01:05
Compact
01:06
Comprehension
01:07
That means
01:08
You can do this
01:09
In English
01:10
Literature
01:11
You can do this
01:12
Comprehensive paragraph
01:13
Comprehensive
01:14
Comprehensive
01:15
Comprehensive
01:16
Comprehensive
01:17
Comprehensive
01:18
Comprehension
01:19
Basically
01:21
What you do
01:22
Is
01:23
That
01:24
We have to do
01:25
Single statement
01:26
And
01:27
How is
01:28
The
01:29
Keep
01:30
Self
01:31
Comprehensive
01:32
I have to
01:33
Do
01:34
For
01:35
To
01:36
This is what we have, this expression is not a variable, this is a variable, this is a expression,
01:45
this is a expression, so this is a expression.
01:49
So what do we need to do? List in the expression.
01:54
List in the expression.
01:56
What do we need to do?
01:58
Then we need to do for loop.
02:01
I remove this for loop.
02:05
For loop is not a problem.
02:10
That's it guys.
02:12
Now print li1.
02:14
Li underscore one.
02:17
Execute.
02:18
Kaboom shakala.
02:20
Same result.
02:21
Over to know.
02:22
Maza aya ki nahi?
02:23
A single line ke anndar hemne kam kar diya.
02:26
This is case one.
02:28
We use the expression to a variable.
02:32
I'm going to divide it.
02:33
I'm going to divide it.
02:34
How many parts are?
02:35
Basically.
02:36
In a list comprehension case one, there are two parts.
02:39
Here I'm going to divide it.
02:41
This part is basically loop.
02:45
However, this part is the expression.
02:47
By the way, this part is the expression.
02:59
Expression.
03:00
Loop.
03:01
Expression.
03:02
Those parts are the two parts, expression and loop.
03:04
Okay, this was the first case.
03:08
In this case, we have to use the expression go single as a variable.
03:11
Now, I have a question for you.
03:14
You can create a list of 0 to 9.
03:18
You can create a list which is 0 to 9 square.
03:23
This is very cool.
03:25
You can say, sir, this is my hand-hand.
03:27
You have to ask questions.
03:29
You can give it a try.
03:33
Otherwise, follow me.
03:35
List 2.
03:35
You can create a list.
03:38
For I in range.
03:41
Range is 10.
03:43
Then, you can create a list of li1.
03:46
You can create a list of li2.
03:49
You can create a list of li2.
03:50
And the i is square.
03:54
And then print it.
03:56
You can create li2.
03:57
Li underscore 2.
04:01
You can execute a list of li2.
04:03
You can use a list of li3.
04:04
This is the very cool.
04:07
This is the very cool.
04:08
Very good.
04:09
This is the case 2.
04:10
If I say that if you want to list comprehension, how do you do it?
04:16
Now look at this loop, I will break it in list comprehension.
04:19
List comprehension, which list has created.
04:22
This list has created.
04:23
Empty created.
04:24
This is the expression.
04:27
You can put it here.
04:30
This is what we have done with variable mic.
04:35
Next is the loop.
04:37
You can put it here.
04:39
This is the redundant code.
04:44
This is the same.
04:46
This is the same.
04:50
This is the same.
04:52
This is the same.
04:54
This is the same.
04:56
This is the same.
04:58
Very easy.
04:59
Now I will iterate on list comprehension.
05:02
This list has two parts.
05:05
This is the same.
05:07
This is the same.
05:08
The next one is the expression.
05:10
This is the loop.
05:12
I want to say that I will say loop.
05:16
This is the expression.
05:18
This is the expression.
05:20
This can be any same.
05:22
You should note that this is a full flag expression.
05:27
Here I am just multiple by two.
05:29
This is two parts.
05:31
At the moment, we have two parts.
05:33
Good.
05:35
The first case is just variable.
05:38
The second one is just expression.
05:41
Now, the third question is about you.
05:42
Ready?
05:44
Now, I will answer one Pepsi bottle.
05:49
The question is this.
05:51
Now, if you want to generate this list,
05:55
you can select even.
05:58
You can filter me.
06:00
I will use the filter function.
06:03
But if you want to use the filter function,
06:05
you can select if you can.
06:07
You can select even number.
06:10
Let's start with Pepsi bottle.
06:13
Otherwise, let's get started.
06:15
Let's create a list list 3.
06:18
This is empty list.
06:19
So, very good.
06:22
Now, what do we do?
06:24
We have basically, we have to do this.
06:27
We have to do this.
06:29
We have to do this.
06:30
We have to filter out.
06:32
Okay.
06:33
Now, let's do this.
06:35
Okay.
06:54
sqr i sqr i sqr
06:58
okay
07:00
I
07:02
I
07:04
I
07:06
I
07:08
I
07:10
I
07:12
I
07:14
I
07:16
I
07:18
I
07:20
I
07:22
I
07:23
I
07:24
ah
07:38
I
07:40
I
07:42
I
07:44
I
07:46
I
07:47
I
07:49
36, 64.
07:50
You have a list.
07:52
Which is?
07:53
Even number.
07:54
Very good.
07:57
Now, what are the code?
08:00
1 loop, 1 conditional, 1 operation.
08:04
3.
08:05
This is case 3.
08:07
Now, this is the list comprehension.
08:10
Now, we will have a list comprehension.
08:12
Magic.
08:13
This is a Pepsi bottle.
08:15
You have to have a Pepsi.
08:17
I will have a Pepsi.
08:20
Now, I will have this list.
08:22
I will have this list.
08:24
I will just go ahead and get to it.
08:26
You can take a machine.
08:27
You can also take a machine to add.
08:29
So, you can turn the sonraki on the other end.
08:31
This is a list.
08:33
I have this list to create.
08:35
Next is the expression.
08:38
If I have an expression, I will remove you.
08:40
I will remove it.
08:42
Now, this expression.
08:44
Now, this expression,
08:46
I told you that the for loop is a good thing.
08:48
Yes, it's a good thing.
08:50
For loop.
08:52
For loop on the for loop.
08:54
For loop on the for loop.
08:56
If we have a for loop on the for loop,
08:58
we can execute it and then execute it.
09:00
Then perform it.
09:02
For loop on the for loop.
09:04
For loop on the for loop.
09:06
This is not a good thing.
09:08
I will do it.
09:10
Here you have.
09:12
This was original code.
09:14
This is a list comprehension code.
09:16
Very very simple.
09:18
Execute and the result will be exactly the same.
09:22
This is a list comprehension result.
09:24
This is a normal result.
09:26
Right?
09:28
I will break down.
09:30
A list comprehension of three parts.
09:34
I will divide them.
09:36
First, second and third.
09:38
First, second and third.
09:40
First, second and third.
09:42
First, first.
09:44
The first is the expression.
09:46
The second is the loop.
09:48
The third is the condition.
09:50
Now, you'll use the condition.
09:52
The condition will not be done.
09:54
The condition will not be done.
09:56
The condition will not be done.
09:58
However, the expression or loop are a list comprehension.
10:02
However, expression and loop
10:04
are a list comprehension
10:06
of a list comprehension.
10:08
This expression and loop
10:11
can be a list comprehension.
10:14
This is clear.
10:16
Very good.
10:18
This is a list comprehension.
10:22
This is clear.
Recommended
3:23
|
Up next
Lecture 79: Sets and Dictionary Comprehension in Python
Nafees AI Lab
5/23/2025
8:19
Lecture 36: Python has Data Structor and Lists
Nafees AI Lab
5/17/2025
9:59
Lecture 37: Python has Data Structor and Lists-II
Nafees AI Lab
5/17/2025
11:19
Lecture 53: Python has for loop and Iterables
Nafees AI Lab
5/20/2025
6:49
Lecture 74: Python has Special Function Map
Nafees AI Lab
5/23/2025
4:03
Lecture 76: Python has Special Function Zip
Nafees AI Lab
5/23/2025
9:15
Lecture 45: Tuple Data Types in Python
Nafees AI Lab
5/18/2025
9:00
Lecture 40: Lists Methods and Rules in Python-II
Nafees AI Lab
5/17/2025
12:42
#20 Python Tutorial for Beginners //While Loop in Python
Top news
9/6/2022
5:49
Lecture 60: Python has some functions
Nafees AI Lab
5/20/2025
8:48
Lecture 42: Dictionary in Python Language
Nafees AI Lab
5/18/2025
8:22
Lecture 39: Lists Methods and Rules in Python.
Nafees AI Lab
5/17/2025
15:38
#19 Python Tutorial for Beginners // If Elif Else Statement in Python
Top news
9/6/2022
7:27
#21 Python Tutorial for Beginners //For Loop in Python
Top news
9/6/2022
10:53
Lecture 68: A Good Exercise in Python Programming
Nafees AI Lab
5/21/2025
4:44
Lecture 61: Why of functions are in Python Language
Nafees AI Lab
5/20/2025
5:10
Lecture 43: Python has Un-changeable dic key
Nafees AI Lab
5/18/2025
5:39
Lecture 54: A Nested For Loop in Python
Nafees AI Lab
5/20/2025
6:57
Lecture 73: Python Best Programming
Nafees AI Lab
5/23/2025
6:16
Lecture 16: Programming Language and Why Python is the Best...!
Nafees AI Lab
5/13/2025
0:27
Making waterproof shoes with duct tape! πποΈ
MetDaan Creative
5 days ago
0:16
When it's your boyfriend's laundry day! π π€£
MetDaan Creative
5 days ago
0:24
Spreading chocolate cream with a plaster! π π
MetDaan Creative
5 days ago
7:40
Mera Mazloom Hussain | Nadeem Sarwar 2020 | 1442
Gham-e-Hussain | ΨΊΩ Ω ΨΨ³ΫΩΨ
6/9/2025
9:38
Episode 1 β The Curious Boy from Ulm (1879-1895)
Nafees AI Lab
6/8/2025