The Wayback Machine - https://web.archive.org/web/20240206095528/https://www.geeksforgeeks.org/python-program-to-swap-ith-with-jth-elements-in-list/
Open In App
Related Articles

Python program to Swap i’th with j’th elements in List

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given a list, the task is to write a Python program to the given list of elements, toggle every i and j elements in the list.

Examples:

Input : test_list = [4, 7, 8, 0, 8, 4, 2, 9, 4, 8, 4], i, j = 4, 8 
Output : [8, 7, 4, 0, 4, 8, 2, 9, 8, 4, 8] 
Explanation : 4 is swapped by 8 at each occurrence.

Input : test_list = [4, 7, 8, 0, 8, 4], i, j = 4, 8 
Output : [8, 7, 4, 0, 4, 8] 
Explanation : 4 is swapped by 8 at each occurrence. 

Method 1: Using loop + conditional statements

In this, we perform the task of toggling using conditional if-else statements, and the task of iteration is performed using a loop.

Python3




# Python3 code to demonstrate working of
# Toggle i,j elements in List
# Using loop + conditional statements
 
# initializing list
test_list = [4, 7, 8, 0, 8, 4, 2, 9, 4, 8, 4]
              
# printing original list
print("The original list is : " + str(test_list))
 
# initializing i, j
i, j = 4, 8
 
for idx in range(len(test_list)):
     
    # perform toggling
    if int(test_list[idx]) == i:
        test_list[idx] = j
    elif int(test_list[idx]) == j:
        test_list[idx] = i
         
# printing result
print("The altered list : " + str(test_list))


Output:

The original list is : [4, 7, 8, 0, 8, 4, 2, 9, 4, 8, 4]
The altered list : [8, 7, 4, 0, 4, 8, 2, 9, 8, 4, 8]

Time Complexity: O(n)
Auxiliary Space: O(1)

Method 2: Using list comprehension + external function

In this, we perform the task of toggling using the external toggle function, and list comprehension is used to iterate through the list.

Python3




# Python3 code to demonstrate working of
# Toggle i,j elements in List
# Using list comprehension + external function
 
# external toggle
def toggle(ele, i, j):
 
    # performing toggle
    if ele == i:
        return j
    elif ele == j:
        return i
    return ele
 
# initializing list
test_list = [4, 7, 8, 0, 8, 4, 2, 9, 4, 8, 4]
              
# printing original list
print("The original list is : " + str(test_list))
 
# initializing i, j
i, j = 4, 8
 
# list comprehension for 1 liner
res = [toggle(ele, i, j) for ele in test_list]
         
# printing result
print("The altered list : " + str(res))


Output:

The original list is : [4, 7, 8, 0, 8, 4, 2, 9, 4, 8, 4]
The altered list : [8, 7, 4, 0, 4, 8, 2, 9, 8, 4, 8]

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method 3: Use the map() function with lambda expression.

Step-by-step approach:

  • Define the toggle function as a lambda expression.
  • Initialize the list to be modified, test_list.
  • Initialize the values of i and j.
  • Apply the map() function to the list using the lambda expression as the argument.
  • Convert the result of the map() function into a list.
  • Print the original list, the values of i and j, and the modified list

Python3




# Using map() function with lambda expression
toggle = lambda ele, i, j: j if ele == i else i if ele == j else ele
 
# initializing list
test_list = [4, 7, 8, 0, 8, 4, 2, 9, 4, 8, 4]
 
# initializing i, j
i, j = 4, 8
 
# applying map() function to the list
res = list(map(lambda ele: toggle(ele, i, j), test_list))
 
# printing original list
print("The original list is : " + str(test_list))
 
# printing result
print("The altered list : " + str(res))


Output

The original list is : [4, 7, 8, 0, 8, 4, 2, 9, 4, 8, 4]
The altered list : [8, 7, 4, 0, 4, 8, 2, 9, 8, 4, 8]

Time complexity: O(n), where n is the length of the list, because we iterate through the entire list once. 
Auxiliary space: O(n), because we create a new list to store the modified elements.


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Last Updated : 02 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials