0

I want min() function to ignore strings,

a = [('position1', 27), ('position2', 25), ('position3', 30)].

min(a) returns ('position1', 27).

Is there a simple way to return ('position2', 25) instead?

pyMikePy
  • 13
  • 4
  • Does this answer your question? [List of Tuples (string, float)with NaN How to get the min value?](https://stackoverflow.com/questions/15148684/list-of-tuples-string-floatwith-nan-how-to-get-the-min-value) – quamrana Nov 11 '20 at 17:15
  • What exactly do you mean by "ignore strings"? What should happen if you compare, for example, `('x', 'x', 0)` to `('a', 1, 'a')`? Do you care about everything non-numeric, or about strings specifically? Do you care how many there are, or are you specifically concerned with a specific index in the tuples? etc. etc. – Karl Knechtel Nov 11 '20 at 17:16

1 Answers1

3

You can just call python's min function but with a special key like so:

a = [('position1', 27), ('position2', 25), ('position3', 30)]
print(min(a, key=lambda e: e[1]))  # should return ('position2', 25)
Kevin Sheng
  • 411
  • 1
  • 5
  • 8