-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))
2
  • Please edit your question and format your code - TUTORIAL Commented Aug 3, 2023 at 10:41
  • 3
    max has an optional key argument: max(a,key = lambda x:x[1]) Commented Aug 3, 2023 at 10:41

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.