pyMikePy
pyMikePy

Reputation: 13

ignore strings when using min() function

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?

Upvotes: 0

Views: 329

Answers (1)

Kevin Sheng
Kevin Sheng

Reputation: 421

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)

Upvotes: 3

Related Questions