Open In App

Python | Modify Equal Tuple Rows

Sometimes, while working with Python Records, we can have a problem in which we need to perform the modification of element records on equality of records. This can have a problem in domains that involve data. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [[(12, 5)], [(13, 2)], [(6, 7)]] test_row = [(13, 2)] 
Output : [[(12, 5)], [(13, 8)], [(6, 7)]] 

Input : test_list = [[(12, 5), (7, 6)]] test_row = [(13, 2)] 
Output : [[(12, 5), (7, 6)]]

Method #1: Using loop + enumerate() 



The combination of the above functions can be used to solve this problem. In this, we apply brute force for performing task of checking for equality and performing required modification. 




# Python3 code to demonstrate working of
# Modify Equal Tuple Rows
# Using loop + enumerate()
 
# initializing list
test_list = [[(12, 5), (13, 6)], [(12, 2), (13, 2)]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing check row
test_row = [(12, 2), (13, 2)]
 
# Modify Equal Tuple Rows
# Using loop + enumerate()
# multiple y coordinate by 4
for idx, val in enumerate(test_list):
    if val == test_row:
        temp = []
        for sub in val:
            ele = (sub[0], sub[1] * 4)
            temp.append(ele)
        test_list[idx] = temp
 
# printing result
print("List after modification : " + str(test_list))

Output : 

The original list is : [[(12, 5), (13, 6)], [(12, 2), (13, 2)]]
List after modification : [[(12, 5), (13, 6)], [(12, 8), (13, 8)]]

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.  loop + enumerate() performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list.

Method #2: Using list comprehension 

This is yet another way in which this task can be performed. In this, we perform the task using list comprehension in a one-liner way similar to the above method. 




# Python3 code to demonstrate working of
# Modify Equal Tuple Rows
# Using list comprehension
 
# initializing list
test_list = [[(12, 5), (13, 6)], [(12, 2), (13, 2)]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing check row
test_row = [(12, 2), (13, 2)]
 
# Modify Equal Tuple Rows
# Using list comprehension
# multiple y coordinate by 4
res = [[(sub[0], sub[1] * 4) for sub in ele] if ele ==
       test_row else ele for ele in test_list]
 
# printing result
print("List after modification : " + str(res))

Output : 
The original list is : [[(12, 5), (13, 6)], [(12, 2), (13, 2)]]
List after modification : [[(12, 5), (13, 6)], [(12, 8), (13, 8)]]

Method 3: Using the map() function with lambda function




# Python3 code to demonstrate working of
# Modify Equal Tuple Rows
# Using map() + lambda function
 
# initializing list
test_list = [[(12, 5), (13, 6)], [(12, 2), (13, 2)]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing check row
test_row = [(12, 2), (13, 2)]
 
# Modify Equal Tuple Rows
# Using map() + lambda function
res = list(map(lambda ele: [(sub[0], sub[1] * 4)
                            for sub in ele] if ele == test_row else ele, test_list))
 
# printing result
print("List after modification : " + str(res))

Output
The original list is : [[(12, 5), (13, 6)], [(12, 2), (13, 2)]]
List after modification : [[(12, 5), (13, 6)], [(12, 8), (13, 8)]]

Time complexity: O(nm) where n is the number of rows and m is the number of tuples in each row. 
Auxiliary space: O(nm) for creating the new list of modified rows.

Method 4: Using the NumPy library. 

Step-by-step approach:




import numpy as np
 
# initializing list
test_list = [[(12, 5), (13, 6)], [(12, 2), (13, 2)]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing check row
test_row = [(12, 2), (13, 2)]
 
# Using NumPy library
# Convert to numpy array
np_array = np.array(test_list)
 
# Use np.where() to compare rows with test_row
# If equal, modify the second element of each tuple in the row
# using np.multiply() and the broadcasted test_row tuple
np_array = np.where(np_array == np.array(test_row),
                    np.multiply(np_array, np.broadcast_to([1, 4], np_array.shape)),
                    np_array)
 
# Convert modified NumPy array back to a list of tuples
res = np_array.tolist()
 
# printing result
print("List after modification : " + str(res))

Output:

The original list is : [[(12, 5), (13, 6)], [(12, 2), (13, 2)]]
List after modification : [[[12, 5], [13, 6]], [[12, 8], [13, 8]]]

Time complexity: O(nm), where n is the number of sublists and m is the number of tuples in each sublist.
Auxiliary space: O(nm), as we create a NumPy array to store the list and perform element-wise comparisons and modifications on the NumPy array.


Article Tags :