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
test_list = [4, 7, 8, 0, 8, 4, 2, 9, 4, 8, 4]
print("The original list is : " + str(test_list))
i, j = 4, 8
for idx in range(len(test_list)):
if int(test_list[idx]) == i:
test_list[idx] = j
elif int(test_list[idx]) == j:
test_list[idx] = i
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
def toggle(ele, i, j):
if ele == i:
return j
elif ele == j:
return i
return ele
test_list = [4, 7, 8, 0, 8, 4, 2, 9, 4, 8, 4]
print("The original list is : " + str(test_list))
i, j = 4, 8
res = [toggle(ele, i, j) for ele in test_list]
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
toggle = lambda ele, i, j: j if ele == i else i if ele == j else ele
test_list = [4, 7, 8, 0, 8, 4, 2, 9, 4, 8, 4]
i, j = 4, 8
res = list(map(lambda ele: toggle(ele, i, j), test_list))
print("The original list is : " + str(test_list))
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
Share your thoughts in the comments
Please Login to comment...