Welcome to PyBeginners ā your go-to place to learn Python programming the easy way! š
Today, we're learning how to compare two lists in Python, a fundamental concept for python programming. This python tutorial will give you the python basics you need to start coding with python examples and learn effectively with python list. 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.
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 this
0:01
channel. Well, this side Raja Lakshmi
0:03
Singum. In today's video, we are going
0:05
to learn something super useful and fun,
0:08
which is how to compare two lists in
0:10
Python. Now, you might be wondering why
0:13
would I even need to compare lists.
0:15
Well, imagine you're making a to-do list
0:18
application and you want to check if the
0:21
tasks the user entered today match the
0:24
tasks they entered yesterday.
0:27
So that's exactly where list comparison
0:30
comes in handy.
0:34
So here's the big idea. We'll take two
0:36
list from the user. Then we'll check if
0:39
they are the same. For two list to be
0:42
the same. So what are the things that we
0:44
have to check is they must have the same
0:47
length and each element should match the
0:50
same order. Sounds simple, right? Let's
0:54
do it step by step.
0:56
So let's start with taking inputs from
1:00
the user
1:07
to list.
1:09
So list one
1:13
input
1:16
just enter the comma separated list.
1:30
and just do dotsplit
1:37
and list to two
1:43
input.
1:47
Enter
1:49
the maybe second
1:53
comma separated list.
2:02
And this will be the first
2:11
comma separated list. And this will be
2:13
the second commaepparated list. And we
2:16
want to split them by
2:19
split method separated with comma. Okay.
2:32
Now let's assume the lists are equal at
2:36
first. So
2:39
let us assume
2:44
the list are equal
2:49
at first.
2:51
So we'll create a variable called equal
2:55
and we'll add true inside that equal
2:58
method.
3:00
So then now it's time to check the
3:03
length of those two lists. So all you
3:06
have to do is first
3:10
check
3:13
if their
3:16
lengths match.
3:20
So if ln of list one
3:27
is equal to ln of list two.
3:34
So when the length of the first three
3:36
len is nothing but the number of items
3:39
that are present in the list like for
3:41
example if the list has three elements
3:43
like 1 2 3 then the number of elements
3:46
in that list is three. So ln will give
3:50
us the quantity of items present in the
3:54
list. So when these two lengths are
3:58
equal. So now
4:01
we will compare elements one by one. So
4:05
now
4:07
compare elements
4:11
one by one
4:14
using a for loop.
4:18
Okay. So how you compare one by one
4:22
especially
4:24
for I in range of len
4:31
of list because how many times you will
4:34
compare obviously the number of times
4:36
the list the number of items are present
4:39
in the list right like for example
4:42
list has 1 2 3
4:46
so the number of times you will count is
4:48
three times which is nothing but the
4:50
maximum count of items present in the
4:53
list which is alen of list one
4:58
right it could be list one or list two
5:01
so if any one of the list is you know
5:05
the size of the list is same then
5:08
obviously if this condition is true that
5:10
means we need just one list length the
5:15
quantity so I'm taking the first one
5:18
itself
5:18
So for in range of ln of list one and
5:23
here you will do it what you will do how
5:26
to access each and every element in the
5:28
list by using the concept of index. So
5:32
if
5:33
list of list one of I don't worry just
5:40
follow the logic we will trace it for
5:42
sure not equal to list
5:46
2 of I
5:50
then if the items are not equal
5:55
obviously the value of equal will be
5:58
false. So equal will now change it to
6:02
false
6:04
and that's it. And now immediately it
6:07
will come out of the loop because we
6:08
don't have to check the other elements.
6:10
Once you see at least one element is not
6:13
matching then you don't have to check
6:14
every element. Once you understood that
6:17
they are not equal you can come out of
6:19
the loop.
6:20
Okay. So this is what the actual logic
6:24
is. And now
6:26
else
6:32
equal
6:34
equal to false.
6:38
Okay. So what's happening here?
6:41
If
6:43
like for example the user gave two
6:45
elements two lists 1 2 and three. Okay.
6:50
If ln of list of one. So what is this
6:52
alien of list of one? The quantity of
6:55
items present in the list. So what is
6:57
the quantity of items present in the
6:59
list? Three. Okay. What is this
7:02
quantity? Three. So if three equal to
7:07
three. Yes, this is true. So now the
7:10
Python program will jump here.
7:13
So now here for i in range of ln of list
7:18
of one. So I obviously if you know about
7:23
the for loop if you know the syntax how
7:25
for loop exactly runs the i actually
7:28
starts with zero okay and the ln of list
7:33
of one is three so is zero in range of
7:37
three true okay so now i value is zero
7:42
so now it's true so now the python jumps
7:45
here if list of i
7:49
See
7:51
list of list one of I what's happening
7:54
here which is nothing but list one of
7:56
zero.
7:59
So list one of 0 is what?
8:05
Listen to me list one of 0 is nothing
8:08
but one. Obviously the concept of index
8:11
is nothing but the position of the
8:14
element in the list. So one is in the
8:18
zeroth position, two is in the first
8:20
position and three is in the second
8:22
position. But the total length of the
8:24
list is three but the positioning the
8:29
indexing starts from zero. So 0 1 2. So
8:34
list one of 0 is 1. Okay. So list one of
8:39
0 is 1.
8:41
And now we want list two of zero.
8:45
So list two of zero
8:50
is
8:52
one itself. So this is list two and you
8:55
want zeroth position which is one. So is
8:58
1 equal to 1 true. But here we are
9:02
checking is 1 not equal to 1. So this
9:06
condition is false. So now the loop goes
9:09
again to the top to the fur loop. Now I
9:13
becomes one
9:16
is one in range of three. Yes.
9:20
If list of one here now the list of zero
9:25
becomes list of one because I value
9:27
changed from 0 to one. So what is the
9:30
list of one value? The first position
9:33
which is nothing but two. Okay. And what
9:37
is the list two of one value? list two
9:40
of one which is nothing but this one. So
9:43
again it will be two. Okay. But two is
9:47
not equal to two is false. So again now
9:52
the Python jumps here and now I value
9:55
becomes two.
9:58
Okay. If list one of two what is list
10:01
one of two? 0 1 2. So this is the list
10:04
one of two which is nothing but three.
10:07
So now it became two and this becomes
10:10
three and list two of two. So list to
10:14
two of two is nothing but this one. So
10:17
it comes here three.
10:20
So three not equal to three. Obviously
10:24
it is false. Then again now the for loop
10:27
the i jumps to three. Is three in range
10:32
of three? No. Right? So now the Python
10:36
program comes out of the loop and it
10:39
will not change the equal false
10:43
and it will come out of the loop. So now
10:45
what is the value of equal?
10:50
I want you to pause. I know you have
10:52
listened to me. I think you have traced
10:54
it with me. Now what is the value of
10:56
equal here? So initially equal value is
10:59
true. So inside this loop it skipped
11:03
this entire if because the condition is
11:06
both are equal. We can see that both are
11:09
equal. So this entire condition is
11:11
skipped. So only for loop
11:14
is running. So if the if condition is
11:18
done so then obviously it will come out
11:22
of the entire loop. So the equal value
11:26
is true itself. So that's the value of
11:29
equal because the list is equal. So
11:32
there is no chance that you can change
11:34
it to false.
11:36
So this is what happened. That's the
11:38
reason I always say I know that you guys
11:40
might fed up saying that use pen and
11:43
paper. But it is really important when
11:44
you are writing each and every line with
11:47
your own mind. Using pen and paper will
11:50
help you definitely uh to think
11:53
logically. Think like computer. Like if
11:56
I write this line, what happens? how
11:58
computer thinks you have to go step by
12:01
step like we did it just now.
12:05
Okay, I will break this down briefly
12:08
once again. So first we asked the user
12:11
to list and we assumed that the list is
12:14
equal. Then we checked the length of
12:17
list. If the lengths are different then
12:20
there is no point of you know going to
12:23
each and every element. So obviously it
12:27
will jump to the else part and it will
12:28
make it false. So if the lengths are the
12:31
same.
12:33
Now we loop through each element
12:36
and we will compare them one by one and
12:39
the moment we find a mismatch we set
12:42
equal to false and we will stop the loop
12:45
by using this break statement. Finally
12:48
we print whether the lists are same or
12:51
not.
12:58
Print the lists are same. Else print the
13:01
lists are different.
13:07
Okay, let's run it.
13:15
1 2 3
13:19
1 2 3
13:22
See if we print the lists are same
13:25
inside the for loop. Every time it is
13:28
running, every time it is printing the
13:30
lists are same. Always always keep that
13:34
in mind. Always try to trace it. So not
13:38
even a single mistake. I did it on
13:40
purpose so that you guys will understand
13:42
the difference.
13:48
Okay, now let's print
13:51
print
13:54
the lists are the same.
14:01
So when they are same if equal
14:08
else
14:10
the lists are
14:14
different.
14:18
So that's it. We have done this kind of
14:22
statement in the previous exercise
14:23
itself. If you guys missed it, I want
14:25
you to go back there, complete that
14:27
exercise and come back here so that you
14:29
will understand because each and in each
14:31
and every exercise I'm explaining you
14:33
something different, something new that
14:36
you guys must get habituated because
14:38
from then on once I explained it I'm
14:41
using it in the next exercise maybe in
14:44
the next programs that we are writing.
14:46
So always please follow each and every
14:49
episode of this tutorial of this entire
14:52
Python beginners course because I would
14:55
suggest you to go through each and every
14:56
minute thing because as a beginner it is
14:58
really important for you to understand.
15:01
The people who are in intermediate stage
15:03
will get the points but the people who
15:05
are really in the beginner stage I want
15:07
you to go through each and everything.
15:10
So that is the reason I'm taking more
15:12
time on each and every episode to you
15:15
know explain it as much as I can.
15:18
So this logic is same as if equal
15:25
just print
15:28
the lists are same
15:35
else
15:37
just print
15:42
the lists are different.
15:48
So this entire logic is same as this
15:51
single line logic. So here we are
15:53
directly doing it inside the print
15:55
itself by using this if condition else.
15:59
So if the condition is true then this
16:01
part will get executed. If condition is
16:04
false then the else part will get
16:06
executed. That's the only difference
16:07
like this is like a bridge. True left
16:11
side, false right side.
16:14
Instead of writing these four lines of
16:16
code, this will optimize the code and
16:18
your code looks more simpler and easier
16:21
to understand. That's the whole point
16:23
is.
16:25
Now let's run this.
16:39
The lists are saying okay.
16:42
What if you want a different list
16:53
the lists are different.
16:56
So finally this is how we will print the
16:58
lists are same or not whether the lists
17:01
are same or not. So this is the entire
17:04
code and see how the comments are you
17:08
know self-explaining the code is
17:10
explained easily explained to even the
17:12
other developer who is looking at our
17:14
code that's the whole point of using the
17:16
comments in every program
17:20
but this is all about the for loop right
17:23
now let's write it in while loop
17:29
okay
17:31
now let's write in while loop.
17:35
So in while loop we will obviously take
17:38
the inputs and we will also keep equal
17:41
equal to true. But the only difference
17:43
is we will initialize the i value to
17:45
zero because in for loop we are using
17:47
the range method. It will initialize it
17:51
by zero itself. But while we have to
17:53
initialize it then it will take from
17:57
then on.
17:59
So I'll just remove this part.
18:03
So here first we'll check if the if the
18:06
len lengths match. So done. And now
18:10
we'll compare the element by element
18:12
using the while loop. So here is the
18:14
entire logic changes.
18:17
So while
18:24
I less than alen of list one. Okay.
18:34
If
18:38
list one of same thing uh there is no
18:41
change. If you observe, if you go
18:43
through the for loop also here we here
18:46
we wrote for I in range of alien of list
18:49
one. But here we are just writing
18:52
while
18:55
I less than
19:00
len of list one.
19:04
That's it.
19:08
while I less than alien of list one. If
19:11
list one of I not equal to list two of
19:16
I, you are writing equal equal to false
19:19
and then you are breaking. But the only
19:21
difference is you have to initialize I
19:24
value zero and also you have to
19:26
increment I value to one for every loop.
19:32
So first round it's zero, next round it
19:34
have to become one, next round it have
19:36
to become two. Don't worry, we'll trace
19:38
it. We'll trace it for sure.
19:43
So that's how you will do it. And then
19:46
you can use else and while also so
19:50
and then you will use this else part. So
19:53
equal equal to false. Okay. Let's run
19:55
this one. Let's trace this one. Okay. If
19:58
ln of list one is nothing but three, we
20:01
are assuming that the value of you know
20:04
the two lists are equal in the beginning
20:06
and we initialize the i value to zero.
20:09
If ln of length list one which is three,
20:13
list two is also three. So 3 equal to 3.
20:17
Is this true? True. Now it jumps here.
20:22
So while i less than what is the value
20:26
of i here? 0 0 less than len of list one
20:31
which is three is 0 less than three true
20:35
so initially I value is zero now if list
20:38
one of zero so what is list one of zero
20:41
the first element not equal to list two
20:44
of zero
20:46
this second list first element is one or
20:49
not equal to one false then again now it
20:53
jumps here now i value becomes
20:57
1 0 + 1 is nothing but one. Is 1 less
21:00
than
21:02
three? True. Now list one of one is
21:08
nothing but the second position two.
21:11
List two of one. The second position is
21:14
two. Is 2 not equal to two? False. So
21:19
again it comes here. It will increments
21:21
the value of i by 1. So this will become
21:25
two.
21:27
Is two less than three? True. So now
21:31
list one of two is nothing but this.
21:35
List two of two is nothing but this one.
21:39
Again is three not equal to three. False
21:43
again. Now I value becomes three. Is
21:46
three less than three? Now listen to me.
21:49
Is three less than three? No. So this
21:53
entire loop will stops. So three less
21:58
than three is false. So it comes out of
22:00
the loop and so it will not even goes to
22:02
this part. So it will just complete this
22:04
part and it will come out of this loop.
22:08
So now what is the value of equal is
22:10
true? Because every time it checks the
22:12
condition it didn't even execute these
22:14
lines. So that means the equal value is
22:17
true. So if equal is true then you will
22:20
print the lists are same. In the same
22:23
way when you give a different value I
22:25
want you to check even for a different
22:28
value so that you will understand why we
22:30
are giving equal equal to false.
22:35
So so
22:39
this one works exactly the same as the
22:41
first code. The only difference is
22:43
instead of for loop we used while
22:48
and here we start from index zero check
22:51
elements one by one and keep moving
22:54
forward until the list ends. If any
22:56
element doesn't match we stop right
22:58
there by using this break. So this is
23:01
how we will complete the logic. So both
23:04
versions solve the same problem just
23:06
using different loops.
23:09
And yeah that's it my friends. I think
23:12
you just learned how to compare two list
23:14
in Python using both fur and while
23:16
loops. If you enjoyed this and found it
23:19
helpful, do me a small favor. Hit the
23:21
like button, share the video with your
23:23
coding buddies, and of course, subscribe
23:25
to the channel so you don't miss more
23:28
beginner friendly Python lessons like
23:29
this one. Remember, every big programmer
23:32
once started as a beginner just like
23:35
you. So, keep practicing, keep coding,
23:37
and I'll see you in the next
#Education
#Scripting Languages
#Computer Education

