Python Program to Swap Two Elements in a List
Last Updated :
26 Sep, 2024
Given a list in Python and provided the positions of the elements, write a program to swap the two elements in the list in Python.
Example: Swap Two Elements in a List using a comma assignment.
Python
my_list = [10, 20, 30, 40, 50]
# Swap elements at index 1 and 3
pos1, pos2 = 1, 3
# Swap the elements
my_list[pos1], my_list[pos2] = my_list[pos2], my_list[pos1]
print("List after swapping:", my_list)
Output
List after swapping: [10, 40, 30, 20, 50]
Switch Position of Two Items in a Python list using Temp Variable
This code defines a function swapPositions that swaps two elements in a list using their positions. The Driver function demonstrates swapping elements at specified positions in the list.
Python
# Swap function
def swapPositions(lis, pos1, pos2):
temp=lis[pos1]
lis[pos1]=lis[pos2]
lis[pos2]=temp
return lis
# Driver function
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3
print(swapPositions(List, pos1-1, pos2-1))
Time Complexity: O(1), for using constant operations.
Auxiliary Space: O(1), for using constant extra space.
Swap Two Elements in a List Using Tuple Variable
Store the element at pos1 and pos2 as a pair in a tuple variable, say get. Unpack those elements with pos2 and pos1 positions in that list. Now, both the positions in that list are swapped.
Python
# Swap function
def swapPositions(list, pos1, pos2):
# Storing the two elements
# as a pair in a tuple variable get
get = list[pos1], list[pos2]
# unpacking those elements
list[pos2], list[pos1] = get
return list
# Driver Code
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3
print(swapPositions(List, pos1-1, pos2-1))
Output:
[19, 65, 23, 90]
Time Complexity: O(1), for using constant operations.
Auxiliary Space: O(1), for using constant extra space.
Swap Two Elements in a List using Inbuilt list.pop()
Pop the element at pos1 and store it in a variable. Similarly, pop the element at pos2 and store it in another variable. Now insert the two popped element at each other’s original position.
Python
# Swap function
def swapPositions(list, pos1, pos2):
# popping both the elements from list
first_ele = list.pop(pos1)
second_ele = list.pop(pos2 - 1)
# inserting in each others positions
list.insert(pos1, second_ele)
list.insert(pos2, first_ele)
return list
# Driver function
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3
print(swapPositions(List, pos1 - 1, pos2 - 1))
Output:
[19, 65, 23, 90]
Time Complexity: O(1), for using constant operations.
Auxiliary Space: O(1), for using constant extra space.
Python Program to Swap Two Elements in a List using Enumerate
Another approach to swapping elements in a list is to use the enumerate function to get the index and value of each element in the list, and then use a loop to find the elements that need to be swapped and swap them.
Python
def swapPositions(lis, pos1, pos2):
for i, x in enumerate(lis):
if i == pos1:
elem1 = x
if i == pos2:
elem2 = x
lis[pos1] = elem2
lis[pos2] = elem1
return lis
List = [23, 65, 19, 90]
pos1, pos2 = 1, 3
print(swapPositions(List, pos1 - 1, pos2 - 1))
# This code is contributed by Edula Vinay Kumar Reddy
This approach has a time complexity of O(n), since it involves looping through the entire list to find the elements to be swapped. The space complexity is O(1), since it only uses a constant amount of additional space to store the elements to be swapped.
Note that this approach can be made more efficient by using a single loop and breaking out of the loop once the elements have been found and swapped.