Python Program to Swap Two Elements in a List

Introduction:

This program is designed to swap two elements in a given list. It provides a function called swap that takes a list and two indices as input and swaps the elements at those indices. The program also includes an example usage to demonstrate how the function can be used.

Algorithm:

1. Define a function called swap that takes three arguments: b (the list in which swapping will occur), index1 (the index of the first element to swap), and index2 (the index of the second element to swap).
2. Check if the provided indices are within the valid range (0 to len(b)-1). If either index is invalid, print an error message and return the original list without making any changes.
3. If the indices are valid, swap the elements at index1 and index2 using Python’s tuple assignment feature: b[index1], b[index2] = b[index2], b[index1].
4. Return the modified list.

Explanation:

1. The function swap take three arguments: b (the list in which swapping will occur), index1 (the index of the first element to swap), and index2 (the index of the second element to swap).
2. The function first checks if the provided indices are within the valid range (0 to len(b)-1). If either index is invalid, it prints an error message and returns the original list without making any changes.
3. If the indices are valid, the function uses Python’s tuple assignment feature to swap the elements. The elements at index1 and index2 are exchanged in a single line: b[index1], b[index2] = b[index2], b[index1].
4. The function then returns the modified list.
5. In the example usage, a list [1, 2, 3, 4, 5] is created, and the indices 1 and 3 are provided for swapping.
6. The swap function is called with the list and the provided indices.
7. If the swap function returns a modified list (i.e., it didn’t encounter any errors), the program prints the list after the swapping operation.

Code :

```    ```
def swap(b,index1,index2):
# Checking two indices are valid or not
if index1 < 0 or index1 >= len(b) or index2 < 0 or index2 >= len(b):
print("Invalid indices provided. Swapping not possible.")
return b
else:
#swapping two indices values
b[index1], b[index2] = b[index2], b[index1]
return b
#Introduction
a=[]
# Read n numbers from the user and store in list-a
n=int(input("Enter the n numbers:"))
for i in range(n):
s=int(input("Enter the number:"))
a.append(s)
#printing list-a
print("Original list:", a)
#Read two indices from the user
x =int(input("Enter first index:"))
y =int(input("Enter second index:"))
# calling swap function
c = swap(a,x,y)
#printing swapped list
print("List after swapping elements:", c)

```
```

Input:

```    ```
Enter the n numbers:5
Enter the number:1
Enter the number:2
Enter the number:3
Enter the number:4
Enter the number:5

```
```

Output:

```    ```
Original list: [1, 2, 3, 4, 5]
Enter first index:1
Enter second index:3
List after swapping elements: [1, 4, 3, 2, 5]

```
```

In the example usage, the original list is [1, 2, 3, 4, 5]. We specify the indices 1 and 3 to swap. After swapping, the list becomes [1, 4, 3, 2, 5], as shown in the output.

The program handles index validation to ensure that the provided indices are valid for the given list. Swapping elements in a list is achieved using the tuple assignment feature of Python, making it a concise solution.