How to Swap First and Last Elements in List Using Python – GATE, Interview & Placement Practice
Jun 1, 2025
Get the full source code of application here:
Show More Show Less View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I'll show you a very
0:04
common question which is asked inside
0:06
Python programs interviews and
0:08
competitive exams and placements so how
0:11
to swap the first and the last element
0:14
in the array or list so if you have a
0:17
list of numbers so in this case I have a
0:19
list of numbers 10 20 30 40 50 so now I
0:23
have the very first element which is 10
0:25
right here the very last element which
0:28
is 50 so what if I want to exchange or
0:31
swap these two numbers so such that 50
0:34
should be replaced here and 10 will be
0:37
replaced here so there is uh not a
0:40
built-in function in Python that you
0:42
want to do this so we actually use the
0:45
concept of negative indexing negative
0:47
indexing in Python simply means that
0:50
negative indexing like this minus one so
0:53
minus one if you write in Python it
0:56
refers to the rightmost element in the
0:57
array as you can see so if I just show
1:01
you right
1:02
here if I print out let's suppose
1:07
uh this is our list and if I say
1:10
negative
1:11
-1 so in other programming languages
1:13
such as C C++ JavaScript this negative
1:16
index is invalid but in Python if you
1:19
write uh array followed by -1 this will
1:22
refer to the rightmost element and then
1:25
this will be -2 this will be -3 -4 -5 so
1:30
if I just print it out let's suppose if
1:32
I run the program right here python app
1:35
9 pi you will see as soon as I write
1:39
negative -1 so rightmost element is
1:41
printed out here you can see 50 is
1:43
printed out if I say here -2 if I change
1:47
this say -2 you will see it will now
1:49
print out
1:51
40 so with this negative indexing
1:54
concept we can easily exchange the first
1:56
and the last element very easily so what
1:59
we are doing right here we are easily
2:02
using the third variable
2:04
temporary you can call this anything we
2:07
are using the temp variable we are
2:09
storing this the very first element
2:11
which is 10 right here in this variable
2:13
so temp is equal to 10
2:17
and then we are using the negative
2:21
indexing right here to get the rightmost
2:23
element and we are storing it to the
2:26
first index which is zero so we are
2:29
transferring this value 50 to
2:32
10 and then we store the value which is
2:36
stored inside temp which is 10 to
2:40
the rightmost element again we can
2:43
access it by minus1 so we are simply
2:45
saying temp this is a threeline logic so
2:49
it is also used to swap two numbers but
2:51
we are using it to swap the first and
2:53
the last element essentially I just
2:56
wanted to tell you about the concept of
2:57
negative indexing which is exist so if
3:00
you execute this program now just
3:02
execute this you will see original list
3:04
was 10 20 30 40 50 now after swapping
3:08
the first and the last element we
3:10
actually 50 is there as the first
3:13
element and 10 has referred to the last
3:16
so in this easy way you can explain
3:18
inside the interview the concept of
3:21
negative indexing and it becomes really
3:24
easy to swap the first and the last
3:26
element in the list or array and also
3:29
check out my website guys
3:30
freemediatools.com
3:32
uh which contains thousands of tools
#Programming
#Scripting Languages
