Modify Equal Tuple Rows in Python


In this article we will learn how to modify equal tuple rows with the help of Python programming. Whenever we talk about data, sometimes we need to modify that data also. So here we will modify equal tuple rows data as per the equality of the records.

Understanding the Problem

The problem at hand is to modify the given tuple for equal rows using python. So we will be having a list of tuples and our task is to modify as per the condition for the particular tuple row in the given list. Let's see the usage of the following example to grasp better this problem −

list_tuple = [[(1, 2), (3, 4)], [(5, 6), (7, 8)], [(9, 10), (11, 12)]] #This is an example tuple list
 [(5, 6), (7, 8)] #This is the test data
 
#add 2 in each number
#output after modifying
[[(1, 2), (3, 4)], [(7, 9), (9, 10)], [(9, 10), (11, 12)]]

Logic for The Above Problem

To solve the given problem we will take an input of a list of tuples, for which we have to perform the given task. After that we will use another test data from the given input list. And we will modify this data based on certain conditions. So here we will perform multiplication for each number in the row. Here for just an example purpose, we will multiply the zero index digit with 2 and first index digit with 4. After multiplying the numbers we will store this data in a result variable to print.

Algorithm

  • Step 1 − First we need to define the list of tuples called list_tuple. This will be the original list for modifying the data.

  • Step 2 − After that we will use test data called test_data.

  • Step 3 − Then we will multiply the first column with 2 and second column with 4 if the items matched in test_data and list_tuple. And store the outcome in the result variable.

Example

# Define original list tuple
list_tuple = [[(2, 40), (4, 15)], [(3, 10), (14, 7)], [(6, 19), (5, 17)]]
 
test_data = [(3, 10), (14, 7)]

# Modify the original data
result = [[(i[0] * 2, i[1] * 4) for i in list] if list == test_data else list for list in list_tuple]

print("List after modification : \n" + str(result))

Output

List after modification :
[[(2, 40), (4, 15)], [(6, 40), (28, 28)], [(6, 19), (5, 17)]]

Complexity

The time complexity is O(1) for modification of the equal tuple rows. The time taken by the algorithm is constant as the size of the tuple is fixed. As we are iterating over the tuple only once so the complexity is constant. And the space complexity for modifying the tuple rows is O(n), here n is the size of the given tuple list. This space is required when we will modify the whole list. In our solution the space required is O(m), here m is the size of the row we are modifying.

Conclusion

In this article we have seen how to modify the equal rows with the help of Python. As we have performed the normal operations in the test data so this approach is efficient for the small size of the data.

Updated on: 16-Oct-2023

14 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements