Python – Replace Elements greater than K
Last Updated :
26 Apr, 2023
Given element list, replace all elements greater than K with given replace character.
Input : test_list = [3, 4, 7, 5, 6, 7], K = 5, repl_chr = None Output : [3, 4, None, 5, None, None] Explanation : Characters are replaced by None, greater than 5. Input : test_list = [3, 4, 7, 5, 6, 7], K = 4, repl_chr = None Output : [3, 4, None, None, None, None] Explanation : Characters are replaced by None, greater than 4.
Method #1 : Using loop
In this, we check for elements greater than K, if found, they are replaced by replace character, otherwise old value is retained.
Python3
test_list = [ 3 , 4 , 7 , 5 , 6 , 7 , 3 , 4 , 6 , 9 ]
print ( "The original list is : " + str (test_list))
K = 5
repl_chr = "NA"
res = []
for ele in test_list:
if ele > K :
res.append(repl_chr)
else :
res.append(ele)
print ( "The replaced list : " + str (res))
|
Output
The original list is : [3, 4, 7, 5, 6, 7, 3, 4, 6, 9]
The replaced list : [3, 4, 'NA', 5, 'NA', 'NA', 3, 4, 'NA', 'NA']
Time complexity: O(n), where n is the length of the test_list. The loop takes O(n) time
Auxiliary Space: O(n), extra space of size n is required
Method #2 : Using list comprehension
This is one-liner way to solve this problem. Similar method as above, just using one-liner.
Python3
test_list = [ 3 , 4 , 7 , 5 , 6 , 7 , 3 , 4 , 6 , 9 ]
print ( "The original list is : " + str (test_list))
K = 5
repl_chr = "NA"
res = [repl_chr if ele > K else ele for ele in test_list]
print ( "The replaced list : " + str (res))
|
Output
The original list is : [3, 4, 7, 5, 6, 7, 3, 4, 6, 9]
The replaced list : [3, 4, 'NA', 5, 'NA', 'NA', 3, 4, 'NA', 'NA']
Method #3 : Using slicing
Python3
test_list = [ 3 , 4 , 7 , 5 , 6 , 7 , 3 , 4 , 6 , 9 ]
k = 5
print ( "The original list is : " + str (test_list))
for i in range ( len (test_list)):
if test_list[i]>k:
test_list = test_list[:i] + [ 'NA' ] + test_list[i + 1 :]
print ( "The replaced list : " + str (test_list))
|
Output
The original list is : [3, 4, 7, 5, 6, 7, 3, 4, 6, 9]
The replaced list : [3, 4, 'NA', 5, 'NA', 'NA', 3, 4, 'NA', 'NA']
Time complexity: O(n)
Auxiliary Space: O(n)
Method 4 : using the map function with lambda function.
steps to implement this approach:
- Initialize the list and print it to show the original list.
- Initialize the value of K and repl_chr.
- Use the map function to apply a lambda function on each element of the list.
- The lambda function will compare each element with K and if it is greater, then it will return the repl_chr otherwise it will return the element itself.
- Convert the result obtained from the map function back to list using list() function.
- Print the result obtained from the above step to show the replaced list.
Python3
test_list = [ 3 , 4 , 7 , 5 , 6 , 7 , 3 , 4 , 6 , 9 ]
print ( "The original list is : " + str (test_list))
K = 5
repl_chr = "NA"
res = list ( map ( lambda ele: repl_chr if ele > K else ele, test_list))
print ( "The replaced list : " + str (res))
|
Output
The original list is : [3, 4, 7, 5, 6, 7, 3, 4, 6, 9]
The replaced list : [3, 4, 'NA', 5, 'NA', 'NA', 3, 4, 'NA', 'NA']
Time Complexity: O(n), where n is the number of elements in the list.
Auxiliary Space: O(n), as we are creating a new list to store the result.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...