Open In App

Python – Replace Non-Maximum Records

Last Updated : 09 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python records, we can have a problem in which we need to perform replace of all the records whose one element is not Maximum. This kind of problem can have application in many domains including day-day programming and web development domain. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [(1, 4), (9, 11)] K = “Non-Max” Output : [‘Non-Max’, (9, 11)] Input : test_list = [(9, 11), (9, 11), (9, 11)] K = “Non-Max” Output : [(9, 11), (9, 11), (9, 11)]

Method #1 : Using loop + map() + filter() + lambda The combination of above functionalities can be used to perform this task. In this, we perform task of filtering using map() and filter function, after that allocation of values to non-maximum elements using brute force approach using loop. 

Python3




# Python3 code to demonstrate working of
# Replace Non-Maximum Records
# Using loop + map() + filter() + lambda
 
# initializing list
test_list = [(1, 4), (9, 11), (4, 6), (6, 8), (9, 11)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = None
 
# Replace Non-Maximum Records
# Using loop + map() + filter() + lambda
res = []
temp = list(filter(lambda ele: ele == max(test_list), test_list))
for ele in test_list:
    if ele not in temp:
        res.append(K)
    else :
        res.append(ele)
 
# printing result
print("The list after replacing Non-Maximum : " + str(res))


Output : 

The original list is : [(1, 4), (9, 11), (4, 6), (6, 8), (9, 11)]
The list after replacing Non-Maximum : [None, (9, 11), None, None, (9, 11)]

  Method #2 : Using list comprehension + map() + filter() + lambda The combination of above functions can be used to perform this task. In this, we perform task using similar to above method, just difference being using list comprehension for allotting K. 

Python3




# Python3 code to demonstrate working of
# Replace Non-Maximum Records
# Using list comprehension + map() + filter() + lambda
 
# initializing list
test_list = [(1, 4), (9, 11), (4, 6), (6, 8), (9, 11)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = None
 
# Replace Non-Maximum Records
# Using list comprehension + map() + filter() + lambda
temp = list(filter(lambda ele: ele == max(test_list), test_list))
res = [ele if ele in temp else K for ele in test_list]
 
# printing result
print("The list after replacing Non-Maximum : " + str(res))


Output : 

The original list is : [(1, 4), (9, 11), (4, 6), (6, 8), (9, 11)]
The list after replacing Non-Maximum : [None, (9, 11), None, None, (9, 11)]

Method 3: Use a for loop to iterate through the list and keep track of the maximum value, then use a list comprehension to replace all non-maximum values with None.

we first find the maximum value in the list using the built-in max() function. Then, we iterate through the list using a for loop and replace each non-maximum value with None using a list comprehension. Finally, we print the resulting list.

Python3




# initialize the input list
test_list = [(1, 4), (9, 11), (4, 6), (6, 8), (9, 11)]
# print the original list
print("The original list is: " + str(test_list))
 
# find the maximum value in the list
max_val = max(test_list)
 
# replace all non-maximum values with None using a list comprehension
res = [max_val if ele == max_val else None for ele in test_list]
 
# print the resulting list
print("The list after replacing Non-Maximum: " + str(res))


Output

The original list is: [(1, 4), (9, 11), (4, 6), (6, 8), (9, 11)]
The list after replacing Non-Maximum: [None, (9, 11), None, None, (9, 11)]

Time complexity: O(n), where n is the length of the input list. This is because we need to iterate through the input list once to find the maximum value and once again to create the output list.
Auxiliary space: O(n), where n is the length of the input list. This is because we are creating a new list res that has the same length as the input list, and we are storing the maximum value in the variable max_val.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads