Hi Guys, In this blog post, we will cover 5 different Python programs to interchange the first and last elements of a list. Each method is explained in detail with examples, flowcharts, and diagrams.
Python is one of the most popular programming languages today because of its simplicity, readability, and powerful built-in features. When learning Python, beginners often start by practicing basic problems such as swapping variables, reversing a list, or finding maximum and minimum values.
One such classic beginner-level problem is:
👉 How do you interchange the first and last elements of a list in Python?
Although this task may look simple, there are actually several different ways to solve it. Exploring multiple solutions helps you understand Python’s features better, from basic indexing to advanced list operations.
📑 Table of Contents
-
Understanding the Problem
-
Program 1: Using a Temporary Variable
-
Program 2: Using Tuple Unpacking
-
Program 3: Using List Slicing
-
Program 4: Using
pop()
andinsert()
-
Program 5: Using Function with Indexing
-
Conclusion
🔎 Understanding the Problem
Let’s say we have a Python list:
nums = [10, 20, 30, 40, 50]
Here:
-
The first element is
10
(index0
). -
The last element is
50
(index-1
orlen(list) - 1
).
We want to swap these two elements so that the list becomes:
[50, 20, 30, 40, 10]
📌 Flowchart of the Swapping Process:
┌─────────────┐
│ Start List │
│ [10,20,30] │
└──────┬──────┘
│
▼
┌─────────────┐
│ Identify │
│ first=10 │
│ last=30 │
└──────┬──────┘
│
▼
┌─────────────┐
│ Swap Values │
│ first↔last │
└──────┬──────┘
│
▼
┌─────────────┐
│ New List │
│ [30,20,10] │
└─────────────┘
🔹 Program 1: Using a Temporary Variable
![Image idea: Side-by-side diagram showing temp
holding the first element, then swapping it with last element.]
def swap_list(lst):
if len(lst) >= 2:
temp = lst[0]
lst[0] = lst[-1]
lst[-1] = temp
return lst
✅ Explanation:
-
Store the first element in a variable (
temp
). -
Move the last element to the first position.
-
Move
temp
to the last position.
Output:
Original: [10, 20, 30, 40, 50]
Swapped : [50, 20, 30, 40, 10]
🔹 Program 2: Using Tuple Unpacking (Pythonic Way)
![Image idea: One-line swap with arrows showing lst[0] <-> lst[-1]
exchange.]
def swap_list(lst):
if len(lst) >= 2:
lst[0], lst[-1] = lst[-1], lst[0]
return lst
✅ Explanation:
-
Python allows multiple assignments in one line.
-
lst[0], lst[-1] = lst[-1], lst[0]
swaps both values directly.
🔹 Program 3: Using List Slicing
![Image idea: Visual showing list [A, B, C, D]
sliced into [last] + middle + [first]
. ]
def swap_list(lst):
if len(lst) >= 2:
lst = [lst[-1]] + lst[1:-1] + [lst[0]]
return lst
✅ Explanation:
-
[lst[-1]]
takes the last element. -
lst[1:-1]
takes the middle elements. -
[lst[0]]
places the first element at the end.
🔹 Program 4: Using pop()
and insert()
![Image idea: Diagram showing first popped out, last popped out, then reinserted in swapped positions.]
def swap_list(lst):
if len(lst) >= 2:
first = lst.pop(0)
last = lst.pop(-1)
lst.insert(0, last)
lst.append(first)
return lst
✅ Explanation:
-
pop(0)
removes the first element. -
pop(-1)
removes the last element. -
Then insert them back in swapped order.
🔹 Program 5: Using Function with Indexing
![Image idea: Flowchart of getting first_index=0
and last_index=len-1
, then swapping.]
def swap_list(lst):
if len(lst) >= 2:
first_index, last_index = 0, len(lst)-1
lst[first_index], lst[last_index] = lst[last_index], lst[first_index]
return lst
✅ Explanation:
-
Define first and last indices.
-
Swap elements at those indices.
🏁 Conclusion
In this blog, we explored 5 different Python programs to interchange the first and last elements in a list:
-
Using a Temporary Variable
-
Using Tuple Unpacking
-
Using List Slicing
-
Using
pop()
andinsert()
-
Using Function with Indexing
👉 The most Pythonic way is tuple unpacking, but the other methods are equally useful to strengthen your understanding of lists.
Comments
Post a Comment