Search code examples
pythonlistmaxminimum

Find maximum value of minimum elements in tuple


If I have a list

[[209, 34], [50, 170], [197, 32], [75, 156], [176, 51], [54, 141], [205, 19], [35, 173]]

How would I go about finding the sublist with the maximum minimum element?

ie, in the case above, it would be index[3] - [75,156] because it's minimum value is greater than the minimum value of all other elements.


Solution

  • It should be as simple as:

    max(list_of_iterables, key=min)
    

    i.e.:

    >>> lst = [[209, 34], [50, 170], [197, 32], [75, 156], [176, 51], [54, 141], [205, 19], [35, 173]]
    [[209, 34], [50, 170], [197, 32], [75, 156], [176, 51], [54, 141], [205, 19], [35, 173]]
    >>> max(lst, key=min)
    [75, 156]
    

    The max (and min) functions work by walking through an iterable and comparing each element in the iterable picking out the biggest (or smallest for min) element. The catch is that the thing compared is the result of the key function applied to the individual element. By default, the key function is just the identity function -- but it can be anything you want. In this case, my key function is min which picks out the minimum value of the sub-list. We then compare the sublists based on their minimum value and pick out the max which is exactly what your question asked for.