## Get min max values from list of tuples

**Closed**. This question is opinion-based. It is not currently accepting answers.

You could use `max`

with `itemgetter()`

function which I think it's `more`

efficient *solution* comparing to `lambda`

, conform to this answer.

from operator import itemgetter max_x = max(mylist,key=itemgetter(0))[0]

**Python,** Method #1 : Using max() + operator.itemgetter() We can get the maximum of corresponding tuple index from a list using the key itemgetter index provided and then mention the index information required using index specification at the end. Method #2 : Using max() + lambda. Method #3 : Using sorted() + lambda. step1: Parse the whole list once, to get x_min and y_min. Complexity is O(N) step2: Store only the indexes of the tuples with x_min and y_min (more than 50% space is saved). Complexity is O(N). If you are looking for just min or max, then never sort such a big list. Sorting can take from complexity is O(N*N) to O(NlogN).

You can use `zip`

here.

In [1]: a=[(1,2),(3,4),(5,6)] In [2]: x,y=zip(*a) In [3]: x Out[3]: (1, 3, 5) In [4]: y Out[4]: (2, 4, 6) In [5]: min(x),max(x) Out[5]: (1, 5) #1 in min and 5 is max in x In [6]: min(y),max(y) Out[6]: (2, 6) #2 is min and 5 is max in y

`timeit`

analysis on `google colab`

.

%timeit minmax(z) #ch3ster's answer 1 loop, best of 3: 546 ms per loop %timeit minmax1(z) #CDJB's answer 1 loop, best of 3: 1.22 s per loop %timeit minmax2(z) #Mihai Alexandru-Ionut's answer 1 loop, best of 3: 749 ms per loop %timeit minmax3(z) #Yevhen Kuzmovych's answer 1 loop, best of 3: 1.59 s per loop

**EDIT:** We can still reduce the execution time if we use `set`

here.

In [24]: def minmax(a): ...: x=set() ...: y=set() ...: for i,j in a: ...: x.add(i) ...: y.add(j) ...: return max(x),min(x),max(y),min(y)

A list of tuples (size of 3 million or 30 lakh) is used for benchmarking.

z=[(randint(0,10),randint(0,10)) for _ in range(3000000)]

`timeit`

analysis as of this edit(4th Feb 12:28 AM) in python 3.7 and windows 10.

In [25]: timeit minmax(z) #Ch3steR's set answer. 384 ms ± 26.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [44]: timeit minmax1(z) #Ch3steR's zip answer. 626 ms ± 3.28 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [39]: timeit minmax2(z) #CDJB's answer max with lambda 1.18 s ± 25.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [40]: timeit minmax3(z) #Mihai Alexandru-Ionut's answer max with itemgetter 739 ms ± 42.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [41]: timeit minmax4(z) #Yevhen Kuzmovych's answer with updating max and min while iterating 1.97 s ± 42.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

*Ch3steR's set answer < Ch3steR's zip answer < Mihai Alexandru-Ionut's answer max and min with itemgetter < CDJB's answer max and min with lambda < Yevhen Kuzmovych's answer with updating max and min while iterating*

when `0<= x,y <=1000000`

List used for benchmarking.

x=[(randint(0,1000000),randint(0,1000000)) for _ in range(3000000)]

`timeit`

analysis.

In [48]: timeit minmax(x) #Ch3steR's set answer. 1.75 s ± 92.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [49]: timeit minmax1(x) #Ch3steR's zip answer. 753 ms ± 31.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [51]: timeit minmax2(x) #CDJB's answer max with lambda 1.29 s ± 115 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [52]: timeit minmax3(x) #Mihai Alexandru-Ionut's answer max with itemgetter 794 ms ± 35.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [53]: timeit minmax4(x) #Yevhen Kuzmovych's answer with updating max and min while iterating 2.3 s ± 164 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

**NOTE :**

*Ch3steR's set* is efficient when `0< x,y < 10`

but when `0< x,y <1000000`

it averages to `1.7s`

I strongly suggest using *Ch3steR's answer with zip* or *Mihai Alexandru-Ionut's answer max and min with itemgetter* when `0< x,y < 1000000`

.

**Find the maximum value in a list of tuples in Python,** Use max() : Using itemgetter() : In [53]: lis=[(101, 153), (255, 827), (361, 961)] In [ 81]: from operator import itemgetter In [82]: max(lis� Sometimes, while working with data, we can have a problem in which we need to find the min/max of tuple elements that are received as values of dictionary. We may have a problem to get index wise min/max. Let’s discuss certain ways in which this particular problem can be solved. Method #1 : Using tuple () + min ()/max () + zip () + values ()

Here is another solution:

max_x, max_y = min_x, min_y = mylist[0] for x, y in mylist: max_x = max(max_x, x) max_y = max(max_y, y) min_x = min(min_x, x) min_y = min(min_y, y)

**How to use Python's min() and max() with nested lists – dbader.org,** But if we simply call min and max on that nested list we don't get the results we Because Python allows us to treat functions as data we can extract the code� Method #1 : Using max () + operator.itemgetter () We can get the maximum of corresponding tuple index from a list using the key itemgetter index provided and then mention the index information required using index specification at the end.

You can use `min()`

and `max()`

with a `key`

argument. To get your required result, you could use:

max_y = max(mylist, key=lambda x: x[1])[1] min_y = min(mylist, key=lambda x: x[1])[1] max_x = max(mylist, key=lambda x: x[0])[0] min_x = min(mylist, key=lambda x: x[0])[0]

**Python Tuple max() Method,** Python tuple method max() returns the elements from the tuple with maximum value. Syntax. Following is the syntax for max() method − max(tuple). Parameters. Python List: Exercise - 60 with Solution. Write a Python program to find a tuple, the smallest second index value from a list of tuples. Sample Solution:- Python Code: x = [(4, 1), (1, 2), (6, 0)] print(min(x, key=lambda n: (n[1], -n[0]))) Sample Output: (6, 0) Pictorial Presentation: Flowchart: Visualize Python code execution:

**Python Tuples (Tutorial With Examples),** Just like Python list, tuples are also the sequence of comma-separated values enclosed Python has built in function max( ) and min( ) to find the maximun and � If the input is a dataframe, then the method will return a series with a minimum of values over the specified axis in the dataframe. By default, the axis is the index axis. 1) Get minimum values of every column : Use min() function to find the minimum value over the index axis. Code :

Python max() and min() – finding max and min in list or array Python examples to find the largest (or the smallest) item in a collection (e.g. list, set or array) of comparable elements using max() and min() methods.

Python tuple method max() returns the elements from the tuple with maximum value. Syntax. Following is the syntax for max() method

It return the item (key/value tuple) with maximum value in dictionary. From it we fetched the key and value fields. If there are multiple keys in the dictionary with maximum value then this solution will not work, because max() function always returns the first occurrence of maximum value. So, what if we want to find all the keys with maximum

##### Comments

- Please explain in
*objective*terms what you mean by "best". - The best answer is one that works. Attempting
*a*solution provides insights to that solution. Also, refactoring is a big part of programming. You learn more about the problem as you attempt to solve it, and you learn more about a particular implementation as you.. write the implementation. Additionally, as the previous comment sais, that depends on your needs. What is considered best for a particular use case may in fact be counterproductive in other cases, even most cases. Don't prematurely optimize. Consider Big-O alogrithms. But often just starting with one will lead you a better answer. - nice.. it working for me. how can i use if it is dictionary?
- How looks your dictionary ?
- Then use this
`max(students_dict.iteritems(), key=itemgetter(1))[0]`

- @MihaiAlexandru-Ionut I used your answer for
`timeit`

analysis on my machine your code's average to`1.3s`

with list of tuples of size 3 million or 30 lakh and interestingly it averages to`750ms`

in`google colab`

with list of tuples of size 3million or 30lakh. And yes,`max`

with`lambda`

is lot slower.`itemgetter`

is way faster with`max`

. - @MihaiAlexandru-Ionut Again edit my answer your code snippet averages to
`750ms`

in my machine. - Great analysis! Could you please try out my solution? :)
- @YevhenKuzmovych I bet your is not optimal
- @jimifiki Totaly agree, just wondering how much worse or better. At this point, it is more of the python's implementation specification than the algorithm itself.
- @Yevhen Kuzmovych I'll add the analysis of your code. Rn I'm in class once I reach my dorm I'll make sure to add it.
- @Ch3steR You = awesome
- Great solution, but
`max`

and`min`

will return a tuple so you need to get an according element afterwords. - @YevhenKuzmovych whoops, thanks :)