Python program to Swap i'th with j'th elements in List
Last Updated :
02 May, 2023
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))
OutputThe 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.
Similar Reads
Python Program to Swap Two Elements in a List
In this article, we will explore various methods to swap two elements in a list in Python. The simplest way to do is by using multiple assignment.Example:Pythona = [10, 20, 30, 40, 50] # Swapping elements at index 0 and 4 # using multiple assignment a[0], a[4] = a[4], a[0] print(a)Output[50, 20, 30,
1 min read
Python program to insert an element into sorted list
Inserting an element into a sorted list while maintaining the order is a common task in Python. It can be efficiently performed using built-in methods or custom logic. In this article, we will explore different approaches to achieve this.Using bisect.insort bisect module provides the insort function
2 min read
Python Program to Split a List into Two Halves
Splitting a list into two halves is a common operation that can be useful in many cases, such as when dealing with large datasets or when performing specific algorithms (e.g., merge sort). In this article, we will discuss some of the most common methods for splitting a list into two halves.Using Lis
4 min read
Swap tuple elements in list of tuples - Python
The task of swapping tuple elements in a list of tuples in Python involves exchanging the positions of elements within each tuple while maintaining the list structure. Given a list of tuples, the goal is to swap the first and second elements in every tuple. For example, with a = [(3, 4), (6, 5), (7,
3 min read
Python Program to replace list elements within a range with a given number
Given a range, the task here is to write a python program that can update the list elements falling under a given index range with a specified number. Input : test_list = [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1], i, j = 4, 8, K = 9 Output : [4, 6, 8, 1, 9, 9, 9, 9, 12, 3, 9, 1] Explanation : List is u
3 min read
Shift Last Element to First Position in list - Python
The task of shifting the last element to the first position in a list in Python involves modifying the order of elements such that the last item of the list is moved to the beginning while the rest of the list remains intact. For example, given a list a = [1, 4, 5, 6, 7, 8, 9, 12], the goal is to sh
3 min read
Python | Move given element to List Start
The conventional problem involving the element shifts has been discussed many times earlier, but sometimes we have strict constraints performing them, and knowledge of any possible variation helps. This article talks about one such problem of shifting Kâs at the start of the list, catch here is it c
6 min read
Python | Sorting list of lists with similar list elements
Sorting has always been a key operation that is performed for many applications and also as a subproblem to many problems. Many variations and techniques have been discussed and their knowledge can be useful to have while programming. This article discusses the sorting of lists containing a list. Le
5 min read
Python Program to repeat elements at custom indices
Given a List, the following program repeats those elements which are at a custom index, these custom indices are provided to it as a separate list. Input : test_list = [4, 6, 7, 3, 1, 9, 2, 19], idx_list = [3, 1, 6] Output : [4, 6, 6, 7, 3, 3, 1, 9, 2, 2, 19] Explanation : All required index element
6 min read
Python | Replace list elements with its ordinal number
Given a list of lists, write a Python program to replace the values in the inner lists with their ordinal values. Examples:Input : [[1, 2, 3], [ 4, 5, 6], [ 7, 8, 9, 10]]Output : [[0, 0, 0], [1, 1, 1], [2, 2, 2, 2]]Input : [['a'], [ 'd', 'e', 'b', 't'], [ 'x', 'l']]Output : [[0], [1, 1, 1, 1], [2, 2
5 min read