Welcome to PyBeginners β your go-to place to learn Python programming the easy way! π
Today, we're checking for empty lists with simple code using some basic Python examples. This python tutorial will give you the python basics you need to start coding and learn about list handling. Subscribe for more coding exercises! π»π¬Whether you're starting from scratch or revisiting the basics, this channel is built just for YOU. We break down Python concepts into bite-sized, beginner-friendly videos using real-life examples, everyday language, and fun visuals.
Learn For loop - https://youtu.be/jrtc5qcFYt4
Learn While loop - https://youtu.be/Idg-OBiDVps -
Think of PyBeginners as your coding buddy β here to help you:
β
Understand the why behind every concept
β
Build confidence, one small step at a time
β
Learn coding without feeling overwhelmed
π Check out more learning resources:
π Blogs: https://www.pybeginners.com/
π Free eBooks & PDFs: https://www.usandopy.com/livros/ π New videos every week β covering Python basics, hands-on exercises, projects, and career tips!
π Subscribe, join the PyBeginners family, and let's make Python simple together π»π¬
#LearnPython #PyBeginners #PythonForBeginners #PythonMadeEasy
Show More Show Less View Video Transcript
0:00
Hey friends, welcome back to our
0:02
beginners friendly Python series. Well,
0:04
this side Raja Lakshmi Singa. Today we
0:07
are going to look at something very
0:08
simple but very very important which is
0:12
how to check if a list is empty in
0:14
Python. I know we have already done this
0:18
before in the previous exercises but
0:20
this time we are going to use the
0:22
concept that we have learned before
0:24
which is regarding the loops
0:27
for loop and while loop.
0:30
So this might sound tiny but trust me
0:33
you'll use this a lot when you start
0:36
building bigger projects.
0:39
Imagine you have a list but you're not
0:42
sure if it has any items in it or not.
0:46
Maybe the user didn't type anything or
0:49
maybe the list is cleared out. So how do
0:52
we check that?
0:54
That's what we are going to learn today
0:56
and we'll do it using two different
0:58
methods. One with for loop and the other
1:01
with while loop.
1:04
Okay, let's start. So all you have to do
1:07
is get the input from the user. So input
1:10
values
1:13
input
1:17
enter elements
1:21
separated by commas
1:29
and just create a list. I'm using my
1:32
list here and you can use any name. So
1:35
I'm doing it input values
1:39
dotsplit. We are splitting them
1:41
separated with commas
1:45
and with firm underscore
1:48
in my list. I'm telling you again guys
1:52
if you jumped directly to this part I
1:55
want you I will suggest you to go back
1:58
see the for loop videos and even the
2:00
while loop videos come back here. I'm
2:02
going to keep that also in the
2:04
description and also attach in the card
2:07
section. Please go check there. See the
2:10
syntax of our loop and why we do it, how
2:13
we do it, everything and even the while
2:15
loops. Then come back here and see this
2:18
exercise. So it'll be easy for you to
2:21
understand what's going on here and how
2:23
we can do it.
2:27
So for underscore in my list. So just
2:30
print
2:33
the list
2:42
contains
2:45
elements.
2:48
And you can just print the list.
2:58
Else you can just see that the list is
3:02
empty.
3:20
And once the list done then you just
3:23
have to come out of this loop. So you'll
3:26
use break here.
3:31
Okay let's walk through this. First I'm
3:33
asking the user to enter some elements
3:35
separated by commas. If the user
3:38
actually types something we have to
3:41
split it into a list. So this will do
3:43
the job. So when we do it if we have
3:48
input list
3:51
that means input values
3:54
then else just give empty. So this line
3:58
is nothing but you're telling the
4:00
computer that if we have input values
4:03
then you can add those input values to
4:05
my list. If we don't have like if we
4:09
have empty list then please add empty
4:12
list to my list. That's the only
4:15
difference. What happens if you do not
4:17
do it? So just
4:20
give it as comment and you see the
4:22
difference. So when you run it and when
4:24
I give empty list
4:27
just click on enter. See the list
4:31
contains empty elements but you see this
4:34
the format you might not like it the
4:37
format because empty list should be
4:39
something like this
4:41
and it is showing something like this.
4:45
If you don't like the format if you want
4:47
to change the format of the input values
4:49
or the list that you want to show to
4:51
them. So you can just use this
4:58
use this statement which is nothing but
5:01
same as the if else condition itself but
5:04
if we have input values do this.
5:09
It is actually something like this. Let
5:11
me show you like if input values
5:16
if we have input values then just update
5:20
my list
5:26
dotsplit the same line
5:33
else
5:38
my test.
5:42
So this line is same as this syntax. So
5:48
if we have input values then this will
5:50
be true. So then my list will get
5:52
updated with these values separated with
5:55
comma using the dotsplit. Okay. Else my
5:59
list will be empty. So this is what
6:02
happening. But instead of writing four
6:04
lines of code, we are just writing it in
6:06
one line to optimize the code. So this
6:09
is called code optimization. The logic
6:12
and the way the program runs everything
6:14
will be same. But we write it in a
6:16
single line of code instead of writing
6:18
it in four lines of code. So that's the
6:21
only difference guys. Don't please don't
6:23
get confused. It is just as simple as
6:26
this syntax. So it is actually a short
6:29
form of writing it. So we can optimize
6:33
the more number of lines like when you
6:35
are dealing with large amount of
6:38
project. So when you have thousands of
6:40
lines then at that time writing the code
6:43
in this manner will help you a lot. The
6:46
code looks shorter and you will
6:48
understand how the things are going on.
6:51
So that is how you will do it.
6:55
So I'm just trying to make it simple for
6:58
you. So this is just a single line
7:00
version of this four lines. That's the
7:03
only difference. So please don't get
7:05
confused. If you still have any doubts,
7:07
just let me know. I will explain it once
7:10
again if you need. So please don't get
7:13
confused. If you I'm asking the user to
7:15
enter some elements separated by commas.
7:17
If the user actually types something,
7:20
then we split it into a list. If the
7:22
user
7:24
just skips it, then you'll get an empty
7:27
list. Okay. So then here comes the fun
7:30
part. The for loop if the list has
7:34
elements
7:36
then the loop will run at least once. So
7:40
that's why we immediately print
7:43
this list contains element because if it
7:46
is running then that means
7:50
the list have elements and you can
7:52
directly break it because our main moto
7:55
is to see whether the list have elements
7:57
or not. If the list have elements then
7:59
it will run at least once. So once it is
8:02
running so we can just break out of the
8:06
loop. We can just come out of the loop.
8:08
We don't have to print every element
8:11
from the list. So we don't have to go
8:13
through each and every element in the
8:15
list. So we will just come out of the
8:17
loop by using this break statement. And
8:20
you will also say if the list is empty
8:22
then you will just print the list is
8:25
empty. Let's run it.
8:34
So now when you give 1 2 3
8:37
so the input takes this if we have input
8:41
values then it will split them into my
8:44
list which is as 1 2 and three. Now it
8:48
comes here for underscore in my list.
8:51
You can just print
8:54
that means for underscore in my list is
8:57
nothing but it is printing
9:00
it is running through each and every
9:02
element. So once it runs
9:07
so the for loop comes here it will check
9:10
whether it have any element or not. So
9:12
it comes here it sees that there is one
9:15
element. It will not even check the
9:17
other two numbers. It will just come
9:19
here. it will see that we have one
9:21
element. Okay. So the list contains
9:24
something. So it will directly break and
9:27
come out of the loop.
9:30
Just it will print the elements and just
9:32
come out of the loop. Why we see 1 2 3
9:35
here? Because we are printing the entire
9:37
list for one loop itself. So that's why
9:41
you see
9:43
if we have empty list
9:45
then it will just say the list is empty.
9:50
the list is empty. So this is what
9:52
happening here. Okay. Now let's do it
9:54
with while loop.
9:59
So for loop you will do the same thing.
10:02
We need input values and if we have
10:05
input values then it will add it to my
10:08
list. If we have empty so then it will
10:10
make it empty my list. And all you have
10:13
to do is every time when you're dealing
10:16
with Y loop always keep that in mind
10:19
that we have to initialize first like
10:23
from where the loop
10:26
starts like for example I =0
10:31
while
10:33
I
10:35
less than len of
10:39
my list
10:46
I less than alien of my list
10:51
print.
10:56
The list contains same thing the list
11:00
contains elements my list.
11:07
Okay. else.
11:10
You can use else in while also I think
11:13
you guys know that
11:18
you can just print the list is empty
11:21
and always use break for one loop
11:24
itself. So now this time we are using
11:27
while loop. So we start with I equal to0
11:32
and then we say while I less than alien
11:35
of my list.
11:38
So I is nothing but zero. Alien of my
11:42
list is nothing but for example if we
11:44
give 1 2 3 values. So what is the alien
11:48
of my list here? It's three because it
11:50
contains three elements.
11:53
So I is less 0 is less than three. True.
11:57
So it comes here. it will just print
11:59
list contains elements and then it will
12:02
print the entire list. So as we know
12:05
that we have so now we understood that
12:09
the list have something in it so it will
12:12
come out of the loop and then the output
12:14
will be this single line. So if the list
12:17
is empty then as usual it comes to else
12:20
part and it will print the list is
12:21
empty. So if there is at least one item
12:24
in the list the condition is true. So we
12:26
print the list. If the list is empty
12:29
then the condition is false. So right
12:31
away the else part will run the list is
12:33
empty. So this is all about while loop.
12:37
So we this is called initialization. So
12:40
this is where we started the loop to
12:43
continue. So the initial value is zero.
12:47
Now i value changes to one next time,
12:50
two next time, three next time. So
12:53
that's all about loop. But here when you
12:56
want to check the list is empty or not
12:59
you can just run the while loop for just
13:02
one loop itself just one round one time.
13:06
So then you can just print if it have
13:08
elements.
13:11
Similarly for for loop also you're doing
13:13
the same thing.
13:15
All right my amazing learners. So this
13:18
is all about checking an empty list
13:19
using fur and while loop. And if today's
13:23
video helped you even a little bit,
13:24
don't forget to hit that like button.
13:26
And if you want more easy to understand
13:28
Python basics, make sure to subscribe to
13:30
the channel and join our growing
13:32
community of beginners who are learning
13:34
together step by step. Remember, every
13:37
coder starts with small steps. And today
13:40
you just took one more. Keep going and
13:43
I'll see you in the next one.

