How to modify equal tuple rows in Python
In this tutorial, we will learn how to modify equal tuple rows in Python. Whenever we work with data in Python, we sometimes need to modify equal tuple rows data based on the equality of the records. There can be issues in such a domain where data is involved.
You can perform this operation using 2 simple methods.
- List comprehension
- Enumerate Method For loop
- List Comprehension
We will use list comprehension which will solve our problem statement in one line.
We first need to make a list of tuples.
tuple_data = [[(6, 10), (3, 16)], [(2, 20), (13, 9)], [(7, 16), (4, 18)]] print("The original list is : " + str(tuple_data)
The above code will generate the following output:
The original list is : [[(6, 10), (3, 16)], [(2, 20), (13, 9)], [(7, 16), (4, 18)]]
Now, we will write the list of tuples for which will help us in checking for the equality of the tuple data.
test_tuple = [(2, 20), (13, 9)] print("The test row is: " + str(test_tuple))
The output for the test_tuple is:
The test row is: [(2, 20), (13, 9)]
Finally, we will use the list comprehension method for checking the equality between original and test data.
output = [[(i[0] * 2, i[1] * 4) for i in list_tuple] if list_tuple == test_tuple else list_tuple for list_tuple in tuple_data] print("List after modification : " + str(output))
The output for the above code is:
List after modification : [[(6, 10), (3, 16)], [(4, 80), (26, 36)], [(7, 16), (4, 18)]]
Entire Code for List Comprehension
tuple_data = [[(6, 10), (3, 16)], [(2, 20), (13, 9)], [(7, 16), (4, 18)]] print("The original list is : " + str(tuple_data)) test_tuple = [(2, 20), (13, 9)] print("The test row is: " + str(test_tuple)) output = [[(i[0] * 2, i[1] * 4) for i in list_tuple] if list_tuple == test_tuple else list_tuple for list_tuple in tuple_data] print("List after modification : " + str(output))
Enumerate Method and For Loop
In this method, we apply Brute-Force for both the checking of the equality of tuple data and also for the change in the data.
Firstly, We need to initialize the list of tuples on which we will perform this change.
tuple_data = [[(6, 10), (3, 16)], [(2, 20), (13, 9)], [(7, 16), (4, 18)]] print("The original list is : " + str(tuple_data))
The output of the above code cell is:
The original list is : [[(6, 10), (3, 16)], [(2, 20), (13, 9)], [(7, 16), (4, 18)]]
Now, we will initialize the tuple list for checking the equality of the records on the above tuple list.
test_tuple = [(2, 20), (13, 9)] print("The test row is: " + str(test_tuple))
The output of the above code is:
The test row is: [(2, 20), (13, 9)]
The checking condition will include a for loop and the enumerate method.
for i, j in enumerate(tuple_data): if j == test_tuple: output = [] for k in j: out = (k[0] * 3, k[1] * 4) output.append(out) tuple_data[i] = output
In the above function, we changed the first element and second element by multiplying them by 3 and 4 respectively. You can perform any kind of function and with any numbers.
The list after the checking condition and modification is:
List after modification : [[(6, 10), (3, 16)], [(6, 80), (39, 36)], [(7, 16), (4, 18)]]
The entire code for the second method is:
tuple_data = [[(6, 10), (3, 16)], [(2, 20), (13, 9)], [(7, 16), (4, 18)]] print("The original list is : " + str(tuple_data)) test_tuple = [(2, 20), (13, 9)] print("The test row is: " + str(test_tuple)) for i, j in enumerate(tuple_data): if j == test_tuple: output = [] for k in j: out = (k[0] * 3, k[1] * 4) output.append(out) tuple_data[i] = output print("List after modification : " + str(tuple_data))
Both the above methods will result in the same output. If you want you can try using different sets of original and test data.
Some links to refer for tuple knowledge:
Leave a Reply