-1
a = [['a', 20], ['b', 36], ['c', 1], ['d', 8], ['e', 55]]

How can I print out the minimum value or maximum value by using Python built-in functions like min() or max()?

The result would look like ['e' , 55] or 55.

for x in a:
    if x[1].isdigit():
        q = x[1]
    print(q)
print(max(q))
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Zaman
  • 1
  • Please [edit](https://stackoverflow.com/posts/76827277/edit) your question and format your code - [TUTORIAL](https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks) – Lorenzo Mogicato Aug 03 '23 at 10:41
  • 3
    `max` has an optional `key` argument: `max(a,key = lambda x:x[1])` – John Coleman Aug 03 '23 at 10:41

1 Answers1

0

If you know that is always at position 1 you can do:

>>> a = [['a',20], ['b',36], ['c',1], ['d',8], ['e' , 55]]
>>> min(a, key= lambda sublist: sublist[1])
['c', 1]

In short the key function needs to returns a value which "sorts" and then returns the entry at the start/end of this list. (in the background of course only the current highest/lowest value is kept)

Daraan
  • 1,797
  • 13
  • 24