I want to compare element by element some tuples within a list of tuples and print the maximum for each comparison. This is my code:
A = [(10, 50, 1), (20, 4, 100), (11, 21, 10), (1, 42, 60), (0, 7, 74)]
indices = [1, 2]
D = (A[i] for i in indices)
for col in range(3):
print(max(D, key = lambda tpl: tpl[col])[col])
But the output is:
20
ValueError: max() arg is an empty sequence
If I modify the generator by materializing the list D, such that D=[A[i] for i in indices], I obtain the desired result, that is:
20
21
100
Why Do I get that error in the former implementation? Is there a way to fix it without materializing the list? Do you have some advice to improve the code?
Thank you