Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Python – Add K to Minimum element in Column Tuple List

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Sometimes, while working with Tuple records, we can have a problem in which we need to perform task of adding certain element to max/ min element to each column of Tuple list. This kind of problem can have application in web development domain. Let’s discuss a certain way in which this task can be performed.

Input : test_list = [(4, 5), (3, 2), (2, 2), (4, 6), (3, 2), (4, 5)], K = 2 
Output : [(4, 5), (3, 4), (4, 4), (4, 6), (3, 4), (4, 5)] 

Input : test_list = [(4, 5), (3, 2), (2, 2), (4, 6), (3, 2), (4, 5)], K = 3 
Output : [(4, 5), (3, 5), (5, 5), (4, 6), (3, 5), (4, 5)]

Method 1: Using min() + loop The combination of above functions can be used to solve this problem. In this, we perform the task of extracting min’s for each column using min() and addition of K using logic compiled in loop. 

Python3




# Python3 code to demonstrate working of
# Add K to Minimum element in Column Tuple List
# Using min() + loop
 
# initializing lists
test_list = [(4, 5), (3, 2), (2, 2), (4, 6), (3, 2), (4, 5)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
# Add K to Minimum element in Column Tuple List
# Using min() + loop
a_min = min(a for a, b in test_list)
b_min = min(b for a, b in test_list)
res = []
 
for a, b in test_list:
    if a == a_min and b == b_min:
        res.append((a + K, b + K))
    elif a == a_min :
        res.append((a + K, b))
    elif b == b_min:
        res.append((a, b + K))
    else :
        res.append((a, b))
 
# printing result
print("Tuple after modification : " + str(res))

Output : 

The original list is : [(4, 5), (3, 2), (2, 2), (4, 6), (3, 2), (4, 5)]
Tuple after modification : [(4, 5), (3, 7), (7, 7), (4, 6), (3, 7), (4, 5)]

Time complexity: O(n), where n is the length of the input list. This is because the program iterates over the input list only once to find the minimum values of the two columns and then iterates over it again to create the modified list.
Auxiliary space: O(n), as the modified list created is of the same length as the input list. The variables used to store the minimum values and the modified list are of constant size and do not depend on the size of the input list.

Method 2: using list comprehension:

This program adds a constant value K to the smallest element in each column of a list of tuples, and returns a new list of tuples with the modified values. It does so by first finding the minimum value in each column using a nested min() function, and then iterating through each element of the input list using a list comprehension to create the modified output list.

Python3




test_list = [(4, 5), (3, 2), (2, 2), (4, 6), (3, 2), (4, 5)]
 
K = 5
 
min_val = min(min(test_list, key=lambda x: x[i])[i] for i in range(2))
 
res = [(a + K, b + K) if a == min_val and b == min_val else
       (a + K, b) if a == min_val else
       (a, b + K) if b == min_val else
       (a, b) for a, b in test_list]
 
print("Tuple after modification: ", res)

Output

Tuple after modification:  [(4, 5), (3, 7), (7, 7), (4, 6), (3, 7), (4, 5)]

Time complexity: O(N), where N is the length of the input list test_list. 
Auxiliary space: O(N), which is the size of the output list res.

Method 3: Using map() and lambda function

In this modified code, we first find the minimum element of the entire list using min(test_list) and unpack it into a_min and b_min. Then, we use map() and lambda function to apply the conditional logic to each tuple in test_list and create a new list of modified tuples. Finally, we convert the map object to a list using the list() function and print the original list and the modified list.

Python3




# initializing lists
test_list = [(4, 5), (3, 2), (2, 2), (4, 6), (3, 2), (4, 5)]
 
# initializing K
K = 5
 
# Add K to Minimum element in Column Tuple List
# Using map() and lambda function
a_min, b_min = min(test_list)
res = list(map(lambda x: (x[0] + K, x[1] + K) if x[0] == a_min and x[1] == b_min else (x[0] + K,
                                                                                       x[1]) if x[0] == a_min else (x[0], x[1] + K) if x[1] == b_min else (x[0], x[1]), test_list))
 
# printing original list and result
print("The original list is : " + str(test_list))
print("Tuple after modification : " + str(res))

Output

The original list is : [(4, 5), (3, 2), (2, 2), (4, 6), (3, 2), (4, 5)]
Tuple after modification : [(4, 5), (3, 7), (7, 7), (4, 6), (3, 7), (4, 5)]

Time Complexity: O(N), where N is the number of tuples in the input list test_list. 
Auxiliary Space: O(N), where N is the number of tuples in the input list test_list.

Method 4: Using list.sort() and index()

Another approach to solve the same problem can be using the list’s sort() method and index() function to find the minimum element in the list. The steps involved in this method are:

  • Sort the list based on the first element of each tuple.
  • Find the index of the first occurrence of the minimum element in the sorted list.
  • Modify the tuple at the found index by adding K to both its elements.

Python3




# Python3 code to demonstrate working of
# Add K to Minimum element in Column Tuple List
# Using list.sort() and index()
 
# initializing lists
test_list = [(4, 5), (3, 2), (2, 2), (4, 6), (3, 2), (4, 5)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
# Add K to Minimum element in Column Tuple List
# Using list.sort() and index()
test_list.sort() # sort based on the first element
min_idx = test_list.index(test_list[0]) # index of first occurrence of minimum element
test_list[min_idx] = (test_list[min_idx][0] + K, test_list[min_idx][1] + K) # modify the tuple
 
# printing result
print("Tuple after modification : " + str(test_list))

Output

The original list is : [(4, 5), (3, 2), (2, 2), (4, 6), (3, 2), (4, 5)]
Tuple after modification : [(7, 7), (3, 2), (3, 2), (4, 5), (4, 5), (4, 6)]

Time complexity: O(n log n) because of the sort() function used to sort the list. The index() function has a time complexity of O(n), but since it is used only once, its impact on the overall time complexity is negligible.
Auxiliary space: O(1) because it modifies the original list in place and does not use any extra space except for the constant space required for the variables used in the code.

Method 5: Using the heapq module

Step-by-step approach:

  • The ‘heapify’ function of the ‘heapq’ module is used to create a heap based on the first element of the tuples in ‘test_list’. This reorders the elements in the list to satisfy the heap property.
  • The ‘heappop’ function of the ‘heapq’ module is used to remove and return the tuple with the minimum first element from the heap, which is stored in the ‘min_tuple’ variable.
  • The minimum element of the tuple is modified by adding ‘K’ to it and the modified tuple is stored in the ‘modified_tuple’ variable.
  • The ‘heappush’ function of the ‘heapq’ module is used to add the modified tuple back to the heap.
  • Finally, the modified ‘test_list’ is printed using the ‘print’ function with a string and the ‘str’ function to convert the list to a string.

Python3




import heapq
 
# initializing lists
test_list = [(4, 5), (3, 2), (2, 2), (4, 6), (3, 2), (4, 5)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
# Add K to Minimum element in Column Tuple List
# Using heapq module
 
# create a heap based on the first element of the tuple
heapq.heapify(test_list)
 
# get the tuple with the minimum first element
min_tuple = heapq.heappop(test_list)
 
# add K to the minimum element of the tuple
modified_tuple = (min_tuple[0] + K, min_tuple[1] + K)
 
# add the modified tuple back to the heap
heapq.heappush(test_list, modified_tuple)
 
# printing result
print("Tuple after modification : " + str(test_list))

Output

The original list is : [(4, 5), (3, 2), (2, 2), (4, 6), (3, 2), (4, 5)]
Tuple after modification : [(3, 2), (3, 2), (4, 5), (4, 6), (4, 5), (7, 7)]

Time complexity: O(N log N) because it involves creating a heap from the input list and performing two heap operations.
Auxiliary space: O(N) because it requires creating a heap to store the input list.


My Personal Notes arrow_drop_up
Last Updated : 16 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials