Open In App

Python – Replace Elements greater than K

Last Updated : 26 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Replace Elements greater than K
# Using loop
 
# initializing list
test_list = [3, 4, 7, 5, 6, 7, 3, 4, 6, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
# initializing repl_chr
repl_chr = "NA"
 
res = []
for ele in test_list:
     
    # replace if greater than K
    if ele > K :
        res.append(repl_chr)
    else :
        res.append(ele)
 
# printing result
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




# Python3 code to demonstrate working of
# Replace Elements greater than K
# Using list comprehension
 
# initializing list
test_list = [3, 4, 7, 5, 6, 7, 3, 4, 6, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
# initializing repl_chr
repl_chr = "NA"
 
# one liner to solve problem
res = [repl_chr if ele > K else ele for ele in test_list]
 
# printing result
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




#input
test_list = [3, 4, 7, 5, 6, 7, 3, 4, 6, 9]
#k value
k=5
print("The original list is : " + str(test_list))
for i in range(len(test_list)):
  #comparing
    if test_list[i]>k:
      #using slicing
        test_list = test_list[:i]+['NA']+test_list[i+1:]
print("The replaced list : " + str(test_list))
#this code is contributed by Asif_shaik


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




# Python3 code to demonstrate working of
# Replace Elements greater than K
# Using map and lambda functions
 
# initializing list
test_list = [3, 4, 7, 5, 6, 7, 3, 4, 6, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
# initializing repl_chr
repl_chr = "NA"
 
# using map function with lambda function to replace elements greater than K
res = list(map(lambda ele: repl_chr if ele > K else ele, test_list))
 
# printing result
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
Previous
Next
Share your thoughts in the comments

Similar Reads