Find the Maximum of Similar Indices in two list of Tuples in Python

When working with two lists of tuples, you may need to find the maximum value at each corresponding index position. Python's zip() function combined with list comprehension provides an elegant solution for this task.

The zip() function pairs up elements from multiple iterables, while list comprehension allows you to iterate and perform operations in a concise manner.

Example

Here's how to find the maximum of similar indices in two lists of tuples ?

my_list_1 = [(67, 45), (34, 56), (99, 123)]
my_list_2 = [(10, 56), (45, 0), (100, 12)]

print("The first list is:")
print(my_list_1)
print("The second list is:")
print(my_list_2)

my_result = [(max(x[0], y[0]), max(x[1], y[1]))
             for x, y in zip(my_list_1, my_list_2)]

print("The maximum value among the two lists is:")
print(my_result)
The first list is:
[(67, 45), (34, 56), (99, 123)]
The second list is:
[(10, 56), (45, 0), (100, 12)]
The maximum value among the two lists is:
[(67, 56), (45, 56), (100, 123)]

How It Works

The solution works by:

  • Pairing tuples: zip(my_list_1, my_list_2) pairs corresponding tuples from both lists
  • Comparing elements: max(x[0], y[0]) finds the maximum of the first elements, max(x[1], y[1]) finds the maximum of the second elements
  • Creating new tuples: Each comparison result forms a new tuple with maximum values at each position

Alternative Approach Using Regular Loop

You can achieve the same result using a traditional for loop ?

my_list_1 = [(67, 45), (34, 56), (99, 123)]
my_list_2 = [(10, 56), (45, 0), (100, 12)]

result = []
for tuple1, tuple2 in zip(my_list_1, my_list_2):
    max_tuple = (max(tuple1[0], tuple2[0]), max(tuple1[1], tuple2[1]))
    result.append(max_tuple)

print("Result using for loop:")
print(result)
Result using for loop:
[(67, 56), (45, 56), (100, 123)]

Conclusion

Use zip() with list comprehension to efficiently compare tuple elements at corresponding positions. The max() function determines the larger value at each index, creating a new list with maximum values preserved.

Updated on: 2026-03-25T17:08:11+05:30

425 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements