Python Program to Swap Two Elements in a List

Python Program to Swap Two Elements in a List | Swapping refers to the exchange of two elements, this is usually done with a list. In this section, we see methods to python swap two elements in a list. The list is a container that stores elements of similar data types.

Swap Two Elements in a List Python

Here, we will see how to swap two elements using the user-defined function, a user-defined function is a function that is defined by the user. In the code below we swap elements in the same list.

Program description:- Write a python program to swap two elements in a list

# Python program to swap two elements in a list

# user-defined function
def swap(l, p1, p2):
   l[p1], l[p2] = l[p2], l[p1]
   return l

# take inputs
l = [1, 2, 3, 4, 5]

# print list
print("List:", l)

# print new list
p1, p2 = 1, 2
print("New List:", swap(l, p1-1, p2-1))

Output:-

List: [1, 2, 3, 4, 5]
New List: [2, 1, 3, 4, 5]

In the above program, we have defined function swap() which takes three parameters list, position1, and position2, then we initialize the list of position 1 and list of position 2 to list of position 2 and lost of position 1. Later we make a list and call swap(). We have swapped 1 element with 2 elements.

Another example of How to Swap Two Elements in a List Python.

# python program to swap two elements in a list

# user-defined function
def swap(l, p1, p2):
   l[p1], l[p2] = l[p2], l[p1]
   return l

# take inputs
l = [1, 2, 3, 4, 5]

# print list
print("List:", l)

# print new list
p1, p2 = 2, 4
print("New List:", swap(l, p1-1, p2-1))

Output:-

List: [1, 2, 3, 4, 5]
New List: [1, 4, 3, 2, 5]

Python Program to Swap Two Elements in a List

In the previous program, swap positions of elements are hardcoded in the program but in this program, the positions will be given by the user.

# python program to swap two elements in a list

# user-defined function
def swap(l, p1, p2):
   l[p1], l[p2] = l[p2], l[p1]
   return l

# take inputs
l = [5, 6, 7, 8, 9]

# print list
print("List:", l)

# take swap position of elements
p1 = int(input("Enter Position 1 Element: "))
p2 = int(input("Enter Position 2 Element: "))

# print new list
print("New List:", swap(l, p1-1, p2-1))

Output for the input values test-case-1:-

List: [5, 6, 7, 8, 9]
Enter Position 1 Element: 1
Enter Position 2 Element: 4
New List: [8, 6, 7, 5, 9]

Output for the input values test-case-2:-

List: [5, 6, 7, 8, 9]
Enter Position 1 Element: 3
Enter Position 2 Element: 5
New List: [5, 6, 9, 8, 7]

Output for the input values test-case-3:-

List: [5, 6, 7, 8, 9]
Enter Position 1 Element: 3
Enter Position 2 Element: 3
New List: [5, 6, 7, 8, 9]

Program to Swap Two Elements in a List Python

This python program performs the same task but in different methods. Here, will see a code to swap the first and last element in list Python. We swap elements in list python by taking elements from the user.

In the code, we have initialized new to an empty list, and also we are taking input from the user and storing it in n, then in the for loop, we take inputs for list elements and append it to empty list new.

# python program to swap two elements in a list

# take inputs
new = []
n = int(input("Enter number of elements in the list: " ))
for i in range(0, n):
   ele = int(input("Enter list element " + str(i+1) + ": " ))
   new.append(ele)
print(new)

# swap elements
temp = new[0]
new[0] = new[n-1]
new[n-1] = temp

# print new list
print("Swapped list:  ")
print(new)

Output for the input values test-case-1:-

Enter number of elements in the list: 4
Enter list element 1: 1
Enter list element 2: 2
Enter list element 3: 3
Enter list element 4: 4
[1, 2, 3, 4]
Swapped list:
[4, 2, 3, 1]

Output for the input values test-case-2:-

Enter number of elements in the list: 7
Enter list element 1: 9
Enter list element 2: 8
Enter list element 3: 7
Enter list element 4: 6
Enter list element 5: 5
Enter list element 6: 4
Enter list element 7: 3
[9, 8, 7, 6, 5, 4, 3]
Swapped list:
[3, 8, 7, 6, 5, 4, 9]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *