Python Program to replace elements of a list based on the comparison with a number

Given a list, the task here is to write a Python program to replace its elements after comparing them with another number here described using K.
For the example depicted in this article, any number greater than K will be replaced with the value given in high and any number less than or equal to K will be replaced with the value given in low.
Input : test_list = [7, 4, 3, 2, 6, 8, 9, 1], low = 2, high = 9, K = 5
Output : [9, 2, 2, 2, 9, 9, 9, 2]Explanation : Elements less than K substituted by 2, rest are by 9.Input : test_list = [7, 4, 3, 2, 6, 8, 9, 1], low =2, high = 8, K = 5
Output : [8, 2, 2, 2, 8, 8, 8, 2]Explanation : Elements less than K substituted by 2, rest are by 8.
Method 1 : Using loop
In this, we perform replacements using conditional statements and iteration is performed using loop.
Program:
Python3
# initializing listtest_list = [7, 4, 3, 2, 6, 8, 9, 1]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 5# initializing low and high Replacement Replacementlow, high = 2, 9res = []for ele in test_list: # conditional tests if ele > K: res.append(high) else: res.append(low)# printing resultprint("List after replacement ? : " + str(res)) |
Output:
The original list is : [7, 4, 3, 2, 6, 8, 9, 1]
List after replacement ? : [9, 2, 2, 2, 9, 9, 9, 2]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2: Using list comprehension
Similar to the method above, only difference is this is a one liner solution and a compact alternative using list comprehension.
Program:
Python3
# initializing listtest_list = [7, 4, 3, 2, 6, 8, 9, 1]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 5# initializing low and high Replacement Replacementlow, high = 2, 9# list comprehension for shorthand solutionres = [high if ele > K else low for ele in test_list]# printing resultprint("List after replacement ? : " + str(res)) |
Output:
The original list is : [7, 4, 3, 2, 6, 8, 9, 1]
List after replacement ? : [9, 2, 2, 2, 9, 9, 9, 2]
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 along with a lambda function.
Step-by-step approach:
- Initialize low and high replacement values: low, high
- Define a lambda function to perform the replacement: replace = lambda x: high if x > K else low.
- Use the map() function to apply the replace function to each element in test_list: res = list(map(replace, test_list)).
- Print the result: print(“List after replacement: ” + str(res)).
Python3
# initializing listtest_list = [7, 4, 3, 2, 6, 8, 9, 1]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 5# initializing low and high Replacement Replacementlow, high = 2, 9# define lambda function for replacementreplace = lambda x: high if x > K else low# apply map function with replace to test_list and convert to listres = list(map(replace, test_list))# printing resultprint("List after replacement: " + str(res)) |
The original list is : [7, 4, 3, 2, 6, 8, 9, 1] List after replacement: [9, 2, 2, 2, 9, 9, 9, 2]
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”.



