Python: Find the min, max value in a list of tuples Python: Find the min, max value in a list of tuples python python

Python: Find the min, max value in a list of tuples


map(max, zip(*alist))

This first unzips your list, then finds the max for each tuple position

>>> alist = [(1,3),(2,5),(2,4),(7,5)]>>> zip(*alist)[(1, 2, 2, 7), (3, 5, 4, 5)]>>> map(max, zip(*alist))[7, 5]>>> map(min, zip(*alist))[1, 3]

This will also work for tuples of any length in a list.


>>> from operator import itemgetter>>> alist = [(1,3),(2,5),(2,4),(7,5)]>>> min(alist)[0], max(alist)[0](1, 7)>>> min(alist, key=itemgetter(1))[1], max(alist, key=itemgetter(1))[1](3, 5)


The zip is not necessary, so this simplifies to map(max, *data) (where data is an iterator over tuples or lists of the same length).