Q:

Python program to swap any two elements in the list

belongs to collection: Python List Programs

0

Python program to swap two elements in a list using the index

We will take a list and two indexes (for swapping ) from the user and then swap the values at the given index in the list.

Example:

Sample Input:
[3, 1, 0, 9, 23, 89, 10] , 2, 5

Output:
[3, 1, 89, 9, 23, 0, 10]

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Method 1: Simply swap values using comma separator

We will swap the elements directly using the common swapping method.

Syntax:

a, b = b, a

Program to swap any two elements in the list

# Python program to swap element of a list

# Getting list from user
myList = []
length = int(input("Enter number of elements: "))
for i in range(0, length):
    val = int(input())
    myList.append(val)

print("Enter indexes to be swapped ")
index1 = int(input("index 1: "))
index2 = int(input("index 2: "))

print("Initial List: ", myList)
# Swapping given elements
myList[index1], myList[index2] = myList[index2], myList[index1]

# Printing list 
print("List after Swapping: ", myList)

Output:

Enter number of elements: 5
10
20
30
40
50
Enter indexes to be swapped
index 1: 2
index 2: 4
Initial List:  [10, 20, 30, 40, 50]
List after Swapping:  [10, 20, 50, 40, 30]

Method 2: Pop values and then inserting them back

One more method to swap values is to pop out values at a given index and then insert them back to the given index.

Algorithm :

  • Get list and index values from the user.
  • Swap values using pop and insert method.
    • val1 = pop(index1)
    • val2 = pop(index2 - 1)
    • insert(index1, val2)
    • insert(index2, val1)
  • Print the list.

Program to swap any two elements in the list

# Python program to swap element of a list

# Getting list from user
myList = []

length = int(input("Enter number of elements  "))
for i in range(0, length):
    val = int(input())
    myList.append(val)

print("Enter indexes to be swapped ")
index1 = int(input("index 1: "))
index2 = int(input("index 2: "))

print("Initial List: ", myList)
# Swapping given element
val1 = myList.pop(index1)
val2 = myList.pop(index2 - 1)
myList.insert(index1, val2)
myList.insert(index2, val1)

# Printing list 
print("List after Swapping: ", myList)

Output:

Enter number of elements  5
10
20
30
40
50
Enter indexes to be swapped
index 1: 1
index 2: 4
Initial List:  [10, 20, 30, 40, 50]
List after Swapping:  [10, 50, 30, 40, 20]

Swapping two values based on their values entered by the user

# Python program to swap element of a list

# Getting list from user
myList = []
length = int(input("Enter number of elements: "))
for i in range(0, length):
    val = int(input())
    myList.append(val)

print("Enter values to be swapped ")
value1 = int(input("value 1: "))
value2 = int(input("value 2: "))

index1 = myList.index(value1)
index2 = myList.index(value2)

print("Initial List: ", myList)

# Swapping given element
myList[index1], myList[index2] = myList[index2], myList[index1]

# Printing list 
print("List after Swapping: ", myList)

Output:

Enter number of elements: 5
10
20
30
40
50
Enter values to be swapped
value 1: 10
value 2: 50
Initial List:  [10, 20, 30, 40, 50]
List after Swapping:  [50, 20, 30, 40, 10]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python List Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to interchange first and last eleme... >>
<< Python program to check if an element is present i...