# How to Swap Elements in List of Python

How to Swap Elements in List of Python | Swapping refers to the exchange of two elements, this is usually done with a list. In this section, we see various methods to python swap list elements. The list is a container that stores elements of similar data types. Also See:- Find Duplicates in List in Python

We will see these below Python program examples:–

• Python Program To Swap Two Elements In a List
• Python Swap Elements Between Two Lists
• Swap First And Last Element In List Python

## Python Program to Swap Two Elements in a List

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.

``````# 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 = [10, 34, 17, 18]

# print new list
p1, p2 = 1, 2
print(swap(l, p1-1, p2-1))``````

Output:

[34, 10, 17, 18]

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.

## How to Swap Elements in List of Python

This python program performs the same task but in different methods. In this program, we will be done with the pop() function in NumPy.

``````# python program to swap two elements in a list

# user-defined function
def swap(l, p1, p2):
ele1 = l.pop(p1)
ele2 = l.pop(p2-1)
l.insert(p1, ele2)
l.insert(p2, ele1)
return l

# take inputs
l = [34, 88, 12, 89]

# print new list
p1, p2 = 0, 2
print(swap(l, p1, p2))
``````

Output:

[12,88,34,89]

## Python Program to Swap Elements Between Two Lists

Now, in the below code to python swap list elements between two lists, in the below code we initialize the first list to integer values and the second list to a string.

``````# Python swap elements between two list

# take inputs
l1 = [0, 9, 8]
l2 = ['z', 'd', 'r']
print("List:")
print(l1, l2)

# print new list
l1[1] , l2[2] = l2[2], l1[1]
print("New List:")
print(l1, l2)``````

Output:

List:
[0, 9, 8] [‘z’, ‘d’, ‘r’]
New List:
[0, ‘r’, 8] [‘z’, ‘d’, 9]

Here, we are trying to swap the second element of the first list with the third element of the first list, hence the output will be as follows.

## Python Program to Interchange First and Last Elements in a List

Here, will see a code to swap the first And last element in list Python. We swap elements in list python by taking input from the user.

``````# Python program to interchange first and last 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:

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

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.

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!