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 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)) |
The original list is : [[(12, 5), (13, 6)], [(12, 2), (13, 2)]] List after modification : [[(12, 5), (13, 6)], [(12, 8), (13, 8)]]
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 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)) |
The original list is : [[(12, 5), (13, 6)], [(12, 2), (13, 2)]] List after modification : [[(12, 5), (13, 6)], [(12, 8), (13, 8)]]
Recommended Posts:
- Python | Modify Equal Tuple Rows
- Python | Remove similar element rows in tuple Matrix
- Python - Remove Similar Rows from Tuple Matrix
- Python | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python - Pairs with Sum equal to K in tuple list
- How to Parse and Modify XML in Python?
- Python program to modify the content of a Binary File
- Modify XML files with Python
- Modify the string such that it contains all vowels at least once
- Modify Numpy array to store an arbitrary length string
- Python | Print unique rows in a given boolean matrix using Set with tuples
- Python Counter| Find duplicate rows in a binary matrix
- Python | Adjusting rows and columns of an excel file using openpyxl module
- Python | Pandas Extracting rows using .loc[]

