Modify Equal Tuple Rows in Python

In this article we will learn how to modify equal tuple rows with the help of Python programming. When working with nested data structures, we sometimes need to selectively modify specific rows that match certain criteria.

Understanding the Problem

The problem is to modify specific tuple rows in a list based on equality matching. We have a list containing sublists of tuples, and we want to modify only those sublists that match our test criteria.

Here's a visual representation of the concept:

Original List: [(1,2), (3,4)] [(5,6), (7,8)] [(9,10), (11,12)] Test Data: [(5,6), (7,8)] After Modification: [(1,2), (3,4)] [(7,8), (9,10)] [(9,10), (11,12)]

Basic Example

Let's start with a simple example where we modify matching rows by adding 2 to each element ?

# Original list of tuple rows
data = [[(1, 2), (3, 4)], [(5, 6), (7, 8)], [(9, 10), (11, 12)]]

# Test data to match
test_row = [(5, 6), (7, 8)]

# Modify matching row by adding 2 to each element
result = []
for row in data:
    if row == test_row:
        modified_row = [(x + 2, y + 2) for x, y in row]
        result.append(modified_row)
    else:
        result.append(row)

print("Original data:")
print(data)
print("\nAfter modification:")
print(result)
Original data:
[[(1, 2), (3, 4)], [(5, 6), (7, 8)], [(9, 10), (11, 12)]]

After modification:
[[(1, 2), (3, 4)], [(7, 8), (9, 10)], [(9, 10), (11, 12)]]

Using List Comprehension

We can make the code more concise using list comprehension ?

# Define original data
tuple_list = [[(2, 40), (4, 15)], [(3, 10), (14, 7)], [(6, 19), (5, 17)]]

# Test data to find and modify
test_data = [(3, 10), (14, 7)]

# Modify matching rows: multiply first element by 2, second by 4
result = [[(i[0] * 2, i[1] * 4) for i in row] if row == test_data else row for row in tuple_list]

print("Original list:")
for i, row in enumerate(tuple_list):
    print(f"Row {i}: {row}")

print("\nAfter modification:")
for i, row in enumerate(result):
    print(f"Row {i}: {row}")
Original list:
Row 0: [(2, 40), (4, 15)]
Row 1: [(3, 10), (14, 7)]
Row 2: [(6, 19), (5, 17)]

After modification:
Row 0: [(2, 40), (4, 15)]
Row 1: [(6, 40), (28, 28)]
Row 2: [(6, 19), (5, 17)]

Multiple Matching Rows

Here's an example where multiple rows match our criteria ?

# Data with duplicate rows
data = [[(1, 3), (2, 4)], [(5, 7), (6, 8)], [(1, 3), (2, 4)], [(9, 11), (10, 12)]]

# Test pattern
test_pattern = [(1, 3), (2, 4)]

# Square all numbers in matching rows
result = [[(x**2, y**2) for x, y in row] if row == test_pattern else row for row in data]

print("Original data:")
for i, row in enumerate(data):
    print(f"Row {i}: {row}")

print("\nAfter squaring matching rows:")
for i, row in enumerate(result):
    print(f"Row {i}: {row}")
Original data:
Row 0: [(1, 3), (2, 4)]
Row 1: [(5, 7), (6, 8)]
Row 2: [(1, 3), (2, 4)]
Row 3: [(9, 11), (10, 12)]

After squaring matching rows:
Row 0: [(1, 9), (4, 16)]
Row 1: [(5, 7), (6, 8)]
Row 2: [(1, 9), (4, 16)]
Row 3: [(9, 11), (10, 12)]

Using Function for Complex Modifications

For more complex modifications, we can use a separate function ?

def modify_tuple_row(row, operation):
    """Apply operation to each tuple in the row"""
    if operation == "double":
        return [(x * 2, y * 2) for x, y in row]
    elif operation == "add_ten":
        return [(x + 10, y + 10) for x, y in row]
    else:
        return row

# Sample data
data = [[(1, 2), (3, 4)], [(10, 20), (30, 40)], [(5, 6), (7, 8)]]
target = [(10, 20), (30, 40)]

# Apply modification
result = [modify_tuple_row(row, "double") if row == target else row for row in data]

print("Modified result:")
for row in result:
    print(row)
Modified result:
[(1, 2), (3, 4)]
[(20, 40), (60, 80)]
[(5, 6), (7, 8)]

Complexity Analysis

Time Complexity: O(n × m), where n is the number of rows and m is the average size of each row for comparison.

Space Complexity: O(n × m) for storing the result list with potentially modified rows.

Conclusion

Modifying equal tuple rows in Python can be efficiently accomplished using list comprehensions with conditional logic. This approach allows selective modification of matching rows while preserving the structure of non-matching data.

Updated on: 2026-03-27T15:23:50+05:30

141 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements