Python Program to Find Second Largest Number in a List Tutorial For Beginners
4K views
Jun 1, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
so in this video I will show you a
0:04
Python program which will able to find
0:06
out the second largest number in a list
0:09
uh it's a question that is asked in many
0:12
interviews and competitive programming
0:15
so I basically show you step by step
0:18
here so I'm using this software python
0:22
twitter.com which actually shows the
0:24
live uh step-by-step visualization of
0:27
the code so this is my program here so
0:30
let me just uh run this step by step so
0:34
first of all this function which is
0:36
second largest function is initialized
0:38
here as you can see it is expecting a
0:41
list of numbers so we have this list of
0:44
numbers right here 12
0:46
451 31 10 so this is a list of numbers
0:50
we are targeting now in the next step
0:53
whenever this list is initialized as you
0:55
can see now the list is initialized
0:58
these are a set of numbers in which we
1:00
need to find out the second largest
1:02
number so now we go to the next step so
1:06
this function is initialized as you can
1:08
see we are there on this step right here
1:11
so this function is
1:12
initialized and now we come to the next
1:16
step so here we are simply checking that
1:18
if the length of the list is smaller
1:22
than two in this case it is not smaller
1:24
than two so now this condition will
1:26
evaluate we go to the next step and here
1:29
we initialize two variables guys which
1:31
is largest and second both will be
1:34
negative infinity so we are initializing
1:37
both these variables to negative
1:39
infinity which is essentially we are
1:42
putting it to a smaller number so this
1:47
can be zero as well but we are putting
1:49
it negative infinity
1:52
so after that we come to the we are
1:55
simply using this for loop here so we
1:58
are on the first element in the for loop
2:00
so the value is 12 here so the num value
2:04
is 12 now we have the logic here if I
2:08
come to the next step we are simply
2:11
having if this if condition that if the
2:14
number that you are targeting is smaller
2:17
than or greater than largest so largest
2:20
as you can see it's put to negative
2:22
infinity so it must be larger so this
2:25
condition will evaluate to true and we
2:27
come to this next step so we actually
2:30
store this second largest number we
2:33
initialize it so this second largest
2:36
number
2:37
becomes to in negative infinity so now
2:41
in the next step we are putting this
2:43
largest number which we have found at
2:46
this moment of time it is 12 so we will
2:49
make this largest to be 12 so in the
2:52
next step largest will become 12 and now
2:55
the cursor will move to the next element
2:57
in the array which will be 45 so now
3:00
again we will check in this if condition
3:02
that if 45 is is greater than 12 because
3:06
up till now the largest number is 12 so
3:09
now in the next step 45 is greater than
3:12
12 so now this will actually make the
3:15
second
3:16
largest
3:18
number so the second largest number will
3:22
become 12 so now if I go to the next
3:26
step you will see now we have number
3:30
45 so second largest will become 12 here
3:34
as you can see and now the largest
3:37
number will
3:38
become 45 because 45 is greater than 12
3:43
so now this will be stored in the
3:45
largest variable so this is the logic
3:47
here as you can see now we come to the
3:50
next step next number in the list which
3:52
is two so two is again is not greater
3:56
than 45 as you can see largest value is
4:00
45 so this will evaluate to false
4:04
so now we need to find out the second
4:08
largest number here so we have this else
4:10
if condition right here so here we are
4:12
checking that if this number which is 2
4:16
is uh greater than
4:18
12 so we are checking it for the second
4:21
largest number so this this condition
4:23
also evaluates to false so we come to
4:26
the next element in the array we move to
4:29
the next element so now the number value
4:31
become 41 so we come to this number in
4:33
the list again 41 is not the largest
4:36
number so our largest number is 45 so 41
4:41
is not greater than 45 so this condition
4:44
will evaluate to false and this then now
4:48
the second condition here just look at
4:51
this condition so now 41 is greater than
4:55
12 this is our second largest number but
4:58
now this 41 is greater than 12
5:02
and 45 is not equal to 41 is not equal
5:06
to 45 so now this needs to be the second
5:09
largest number so now we need to update
5:11
this second variable so now we come to
5:16
this step so now second variable second
5:19
largest number become now
5:21
41 so now we come to the next number in
5:24
the list which is
5:26
31 so now number becomes 31 so 31 is not
5:30
greater than
5:32
45 so this condition will evaluate to
5:35
false and 31 is also not the second
5:39
largest number as it's not greater than
5:43
41 so this also evaluates to false we
5:46
come to the last element in the list
5:48
which is 10 so this will also evaluates
5:51
to false in both the conditions
5:55
and
5:56
now the for loop will
5:59
end as you can see it is end and uh now
6:03
we'll simply return the second largest
6:05
number which is 41 from this function so
6:09
return value is
6:10
41 and then we simply print out the
6:14
second largest number is 41 so this is
6:16
essentially the program guys uh we
6:19
define this
6:21
function hopefully you understood this
6:23
condition
6:26
so thank you very much guys for watching
6:28
this
6:30
video and also check out my website
6:34
freemediattools.com
6:36
uh which contains thousands of tools