Skip to main content

Python Program to Swap Two Elements in a List (5 Different Methods)

In this blog post, we will cover 5 different Python programs to swap two elements in a list of 10 integers. Each program will be explained in detail with code, step-by-step execution, and outputs. By the end of this article, you will know not just one but five different approaches to swapping elements in Python lists.

Python is a beginner-friendly yet powerful programming language that is widely used in software development, data science, machine learning, artificial intelligence, and even web applications. One of the first things a programmer learns when starting with Python is how to manipulate lists—one of the most important data structures.

Among the various list operations, swapping two elements is a classic exercise for beginners. At first glance, it might look like a trivial problem, but solving it in multiple ways helps you:

  • Understand Python’s built-in features like indexing, slicing, and tuple unpacking.

  • Learn about different methods to manipulate list elements.

  • Build a strong foundation for solving bigger problems involving arrays and matrices.

Whether you are a beginner preparing for coding interviews or someone brushing up on Python basics, this guide will be useful for you.

📑 Table of Contents

  1. Understanding the Problem

  2. Program 1: Swap Using a Temporary Variable

  3. Program 2: Swap Using Tuple Unpacking (Pythonic Way)

  4. Program 3: Swap Using pop() and insert()

  5. Program 4: Swap Using Index Referencing

  6. Program 5: Swap Using Arithmetic Operations (Without Temp Variable)

  7. Conclusion

🔎 Understanding the Problem

Before diving into code, let’s clearly define the problem.

We have a list of 10 integer values. For example:

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

If we want to swap the 3rd element (value 3) with the 8th element (value 8), then the list should change into:

[1, 2, 8, 4, 5, 6, 7, 3, 9, 10]

The goal is to write Python programs that can swap two elements at given positions in different ways.

🔹 Program 1: Swap Using a Temporary Variable

This is the traditional method taught in programming fundamentals. It uses a temporary variable to hold one value while swapping.

✅ Code

def swap_elements(lst, pos1, pos2):
    temp = lst[pos1]
    lst[pos1] = lst[pos2]
    lst[pos2] = temp
    return lst

nums = [1,2,3,4,5,6,7,8,9,10]
print("Original:", nums)
print("Swapped :", swap_elements(nums, 2, 7))  # swap 3rd and 8th element

✅ Explanation

  1. Take the element at pos1 (index 2, value 3) and store it in a temporary variable temp.

  2. Replace element at pos1 with the element at pos2 (index 7, value 8).

  3. Place the temporary variable (3) at position pos2.

✅ Output

Original: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Swapped : [1, 2, 8, 4, 5, 6, 7, 3, 9, 10]

This method is clear and universally applicable (works in C, Java, C++, etc.).

🔹 Program 2: Swap Using Tuple Unpacking (Pythonic Way)

Python offers a neat trick called tuple unpacking that allows swapping in just one line.

✅ Code

def swap_elements(lst, pos1, pos2):
    lst[pos1], lst[pos2] = lst[pos2], lst[pos1]
    return lst

nums = [10,20,30,40,50,60,70,80,90,100]
print("Original:", nums)
print("Swapped :", swap_elements(nums, 0, 9))  # swap first and last

✅ Explanation

  • lst[pos1], lst[pos2] = lst[pos2], lst[pos1] swaps values directly.

  • No temporary variable is needed.

✅ Output

Original: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Swapped : [100, 20, 30, 40, 50, 60, 70, 80, 90, 10]

This is the most Pythonic and efficient approach.

🔹 Program 3: Swap Using pop() and insert()

Here, we use Python list methods pop() and insert().

✅ Code

def swap_elements(lst, pos1, pos2):
    first = lst.pop(pos1)
    second = lst.pop(pos2-1)  # adjust index after first pop
    lst.insert(pos1, second)
    lst.insert(pos2, first)
    return lst

nums = [5,10,15,20,25,30,35,40,45,50]
print("Original:", nums)
print("Swapped :", swap_elements(nums, 1, 8))  # swap 2nd and 9th element

✅ Explanation

  1. pop(pos1) removes and returns the first element to be swapped.

  2. pop(pos2-1) removes the second element (adjust index since the list shrinks after first pop).

  3. Insert them back in swapped order using insert().

✅ Output

Original: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
Swapped : [5, 45, 15, 20, 25, 30, 35, 40, 10, 50]

This method is interesting but less efficient for very large lists due to shifting elements.

🔹 Program 4: Swap Using Index Referencing

This is a simple reusable function where you explicitly use indices.

✅ Code

def swap_elements(lst, pos1, pos2):
    if pos1 < len(lst) and pos2 < len(lst):
        lst[pos1], lst[pos2] = lst[pos2], lst[pos1]
    return lst

nums = [2,4,6,8,10,12,14,16,18,20]
print("Original:", nums)
print("Swapped :", swap_elements(nums, 4, 5))  # swap 5th and 6th elements

✅ Explanation

  • We check if both positions are valid.

  • Swap the two elements using tuple unpacking.

✅ Output

Original: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Swapped : [2, 4, 6, 8, 12, 10, 14, 16, 18, 20]

This method is safe (won’t throw errors for invalid indices).

🔹 Program 5: Swap Using Arithmetic Operations (Without Temp Variable)

This is a classic trick where you swap values without using a temporary variable or tuple unpacking.

✅ Code

def swap_elements(lst, pos1, pos2):
    lst[pos1] = lst[pos1] + lst[pos2]
    lst[pos2] = lst[pos1] - lst[pos2]
    lst[pos1] = lst[pos1] - lst[pos2]
    return lst

nums = [11,22,33,44,55,66,77,88,99,110]
print("Original:", nums)
print("Swapped :", swap_elements(nums, 3, 6))  # swap 4th and 7th element

✅ Explanation

Suppose we want to swap 44 and 77:

  1. lst[pos1] = 44 + 77 = 121

  2. lst[pos2] = 121 - 77 = 44

  3. lst[pos1] = 121 - 44 = 77

✅ Output

Original: [11, 22, 33, 44, 55, 66, 77, 88, 99, 110]
Swapped : [11, 22, 33, 77, 55, 66, 44, 88, 99, 110]

This method is fun but risky if the numbers are too large (may cause integer overflow in some languages, though Python handles big integers safely).

🏁 Conclusion

In this blog, we explored 5 different ways to swap two elements in a list of 10 integers using Python:

  1. Using a Temporary Variable – Beginner-friendly and universal.

  2. Using Tuple Unpacking – The most Pythonic and concise way.

  3. Using pop() and insert() – Demonstrates list manipulation methods.

  4. Using Index Referencing – Safe and reusable.

  5. Using Arithmetic Operations – A clever trick without extra space.

👉 If you are a beginner, start with the temporary variable approach.
👉 If you want to write clean and professional Python code, use tuple unpacking.

Learning multiple solutions not only helps in interviews but also improves your ability to solve problems in creative ways.

💡 Pro Tip: Try modifying these programs to swap any two elements given by the user (using input()), or extend it to swap multiple pairs at once.

Comments