Welcome to PyBeginners β your go-to place to learn Python programming the easy way! π
Today, we're exploring how to check if a list is empty in Python, a fundamental concept in Python programming. This video will provide python examples to help beginners understand this concept, further improving their python coding skills.
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.
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/
πΌ LinkedIn: https://www.linkedin.com/newsletters/pybeginners-7267881715929415681/
π 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 π»π¬
Show More Show Less View Video Transcript
0:00
Hey everyone, welcome back to our Python
0:02
series of beginners. In the previous
0:04
episodes, we have gone through list and
0:06
Python and its built-in functions. So
0:08
today we are going to deal with the
0:10
interesting programs where we can
0:12
understand and work on list and where we
0:15
can get more practical knowledge over
0:17
list.
0:22
So today we are going to do something
0:24
super important where we are going to
0:26
check if a list is empty or not in
0:29
Python. It sounds simple right? But
0:31
trust me this tiny concept is something
0:34
you'll use a lot as you go deeper into
0:37
coding.
0:39
So here's the plan. First we'll ask the
0:41
user to enter some elements separated by
0:45
comma and then we'll finally turn that
0:48
into a list.
0:50
and we'll check if the list is empty or
0:52
not using an else condition.
0:56
All right, let's write the code step by
0:59
step. First,
1:03
step one
1:09
as
1:11
user input.
1:17
Okay. and user input
1:27
enter the elements. So we are asking the
1:30
user to give the input elements
1:34
separated
1:37
with
1:38
commas so that user will understand that
1:41
we are going to create a list. Okay. And
1:45
step two
1:49
is to create a list.
1:53
Okay, let's create a list. So my list
1:56
equal to
1:59
user input dotsplit
2:03
and then separated with comma. So here
2:07
we are using a function called split.
2:09
Split. Let us see what it actually do.
2:13
So you can just say print my list.
2:18
And now when you run this one, it will
2:21
ask me the enter the elements separated
2:24
with commas. Okay. 1 2 3.
2:29
And now when you enter, see it is taking
2:32
the 1 2 3 as a strings because we didn't
2:35
give anything here. And then it is
2:39
storing them separated with comma. So
2:41
what this split is doing is it is taking
2:43
the elements from the input and then it
2:48
is checking whether it is the data type
2:50
of the you know input it but like is
2:53
that a string or is that input float
2:55
whatever the value then it is adding
2:59
dotsplit like it is splitting each and
3:01
every item which is separated with
3:04
comma. Okay. What if you give hyphen
3:08
here and now
3:11
hyphen
3:13
or maybe this one?
3:18
Okay, when you do this
3:22
now you have to give one 2 3. See it is
3:27
still taking the input separated with
3:29
comma. So you are using this as a limit
3:33
of separation like okay if there is any
3:35
hyphen then this is this is one element
3:38
and then it will check it will come here
3:40
and it will take it as one element then
3:42
it will take it as one element. So we
3:45
are using some medium to separate the
3:47
elements. It could be comma or any other
3:50
even the space empty space you can just
3:53
give empty space
3:59
or maybe so when you give this
4:08
one space 2 space 3 see it is still
4:11
taking it as a list so you just have to
4:13
give some medium so it'll be easy for
4:16
the Python program to understand that
4:18
hey this is one element this is one
4:19
element this is one element as we are
4:21
the humans we can understand we can see
4:23
the input you know we can sense it and
4:25
we can do it but how computer will
4:27
understand so that is why you have to
4:29
give some medium some space or any comma
4:32
or any special character so that it can
4:35
understand in the same way we have to
4:37
expect the user to give the input in the
4:40
same way so that is why we are giving
4:44
separated with comma
4:49
And here you will give comma. Okay.
4:54
So we are actually taking the input from
4:57
the user using the split method. So we
5:00
are taking the input we are using split
5:04
and then what if the input is empty.
5:10
Okay. Sometimes user might not give
5:13
anything. So what if the list is empty?
5:16
So here we are going to check the
5:18
condition if
5:21
if there is any input
5:33
if there is any input.
5:36
So as you guys know about boolean so if
5:39
condition will check whether the
5:41
condition is true or false. If this is
5:43
true then it will go to the else block.
5:45
Right?
5:48
If this is true then it will execute
5:52
this one. If not it will come here to
5:54
the else block. So what you're doing
5:56
here if and you know that other than
6:00
zero whatever the value that give will
6:02
be considered as true in programming
6:04
language. If you go and check the
6:06
previous episodes and if you if you guys
6:08
are following the previous episodes, you
6:11
guys now know that if there is nothing
6:15
like if there is a zero then it is
6:17
false. If there is one then it is true.
6:19
Other than one if there is any other
6:21
value then it is true. True itself. It
6:24
could be one or two or any list or any
6:27
item whatever is present we will
6:29
consider it as true. If there is
6:31
nothing, if there is zero even empty
6:34
list. So if there is nothing like zero,
6:37
then we will consider it as false. So
6:39
what if the user input is empty? So then
6:42
it will go to the else block. Pass is uh
6:46
actually a keyword where we can
6:48
sometimes you don't know what logic to
6:50
be written in that particular block. So
6:54
at the time you want to make this
6:57
program run even without writing
6:59
anything. So at that time it will not
7:02
throw error on us saying that there is
7:05
something wrong.
7:09
So it will eventually execute though you
7:11
give pass. So it will just pass the
7:13
statement. It will not show any error
7:16
something like that. So that's why I
7:17
used pass here. So now if user input
7:22
okay if user input is true then you are
7:24
going to add my list into it. It could
7:27
be 1 2 3 or maybe any other. Okay. But
7:31
what if the list is empty? So when list
7:34
is empty then you will just append my
7:37
list with the empty list itself. So it
7:40
will go to the else block. Like for
7:42
example I hope I'm clear with this. I'm
7:46
trying to explain trying to be better.
7:49
Okay.
7:51
So for example the user gave input
7:57
1A 2a 3. So it is something it is not
8:01
zero. It is not an empty list. So it is
8:03
something. So if something that means it
8:07
is true then the program will understand
8:10
okay this statement is true. Then I will
8:13
come here and I will add that list into
8:18
my list. So it is adding that 1 2 3 into
8:21
your list and then what happening you're
8:25
just printing. So obviously these two
8:28
lines will get executed. So once it get
8:31
executed then it will directly print my
8:33
list. It will skip the else block. We
8:36
have already gone through this. We know
8:38
the syntax of if statement before. Okay.
8:42
So this is what's happening. What if the
8:44
user gives empty list? So in programming
8:48
empty list empty uh space zero are
8:53
considered as false. So if condition
8:57
user input is empty that means this is
9:00
false. So obviously this block is
9:03
skipped and now the program goes to the
9:05
else part. And what we written in the
9:08
else part we actually give an empty list
9:12
to my list. So I give empty item in the
9:15
list.
9:16
So obviously now it will print an empty
9:20
element. So this is what we are doing.
9:23
So if statement you don't have to give
9:25
like this like
9:28
you don't have to give something like
9:30
this. You don't have to do that like if
9:34
the input is this only then I can do
9:38
that's fine. If input is 1 2 3 only I
9:41
will perform this operation. This also
9:43
will work. But some cases you don't have
9:46
to do give the entire condition. You
9:48
just want to check whether it is true or
9:50
false. Depends upon the condition. So in
9:53
this case I just want I just need some
9:56
input. It could be anything. It could be
9:58
1 2 3 3 4 5 maybe or any ABC it could be
10:02
anything. So I just want some input so
10:05
that something is satisfying here. So I
10:07
just gave user input. I hope I'm clear.
10:10
If you guys still have any doubts after
10:11
seeing this episode, you can drop it. It
10:13
will even explain it on pen and paper.
10:16
So it will be easy even for you.
10:19
So if this is true then it will update
10:21
my list with the elements that user
10:24
gave. If the list is empty then it will
10:25
update it with empty list itself. So now
10:28
we have to check whether the list is
10:30
empty or not. So
10:35
step three,
10:40
check
10:41
if the list is empty
10:47
or not.
10:51
So if
10:54
my list is empty, so so this is actually
10:59
considered as empty string, right? empty
11:01
list, right? So if my list is empty,
11:06
then print
11:11
list is empty.
11:20
If the list is not empty, then
11:26
print
11:31
list contains elements
11:39
my list. So now it will check whether
11:42
the list is empty or not.
11:49
Like for example 1, 2, 5, 6. List
11:56
contains elements 1 2 5 6. Okay.
12:05
What if you give empty list?
12:09
I gave empty. So it is showing the list
12:11
is empty.
12:13
Okay. So finally to check whether the
12:16
list is empty or not, we used if else
12:18
condition. So if the list is empty, if
12:23
the list is this empty, then print list
12:26
is empty otherwise list contains element
12:29
and show just the items whatever in the
12:32
list.
12:34
So there you go. You have just learned
12:36
how to check if a list is empty or not
12:38
in Python. It might feel tiny right now
12:41
or it might also feel heavy for you.
12:46
I'm saying it once again. If you feel it
12:49
not comfortable like if you don't
12:51
understand it just give me a comment.
12:54
I'm going to make using the pen and
12:57
paper because I literally trust working
13:00
on pen and paper rather than you know
13:02
typing and explaining the logic.
13:06
So I want you to sit and trace it
13:08
yourself and then you can understand the
13:10
magic the magic behind the scenes of
13:13
each and every line which has been run
13:15
by the interpreter. So it might um but
13:19
you know this skill will come in handy
13:21
when you're dealing with bigger problems
13:23
like validating user input or when
13:25
you're handling the missing data. So I
13:28
want you to uh stress that keep
13:30
practicing these basics because they are
13:33
the building blocks of everything else
13:35
because that is what we'll do later. All
13:38
right?
13:40
So keep practicing. They are going to be
13:43
the building blocks of everything else
13:44
we'll build later. So, if you enjoyed
13:48
this, give it a like, subscribe if
13:50
you're new, and I'll see you in the next
13:52
one. And yeah.

