Skip to content
Advertisement

How do i get the min and max value of tuples

How do i get the code to return 2 (lowest of all) and 29 (the biggest one)

intervals = [
    (2, 18),
    (2, 15),
    (5, 28),
    (10, 14),
    (11, 29),
    (6, 17),
    (3, 7),
    (8, 22)
]

z = max(intervals)
print (z)

Advertisement

Answer

When searching for the min, you want to first take the min of each tuple and then take the min through those. You can achieve this with map which has the advantage of not having to create an extra list in the process.

z_max = max(map(max, intervals))
z_min = min(map(min, intervals))
Advertisement