Python - Finding Max Value in the second column of a nested list -


so have list this

alkaline_earth_values = [['beryllium', 4],['magnesium', 12],['calcium', 20],['strontium', 38],['barium', 56], ['radium', 88]] 

if use max(list) method, return answer strontium, correct if trying find max name, i'm trying return element integer highest.

any appreciated.

max(alkaline_earth_values, key=lambda x: x[1]) 

the reason works because key argument of max function specifies function called when max wants know value maximum element searched. max call function each element in sequence. , "lambda x: x[1]" creates small function takes in list , returns first (counting starts zero) element. so

k = lambda x: x[1] 

is same saying

def k(l):   return l[1] 

but shorter , nice use in situations this.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

sql server - python to mssql encoding problem -

windows - Python Service Installation - "Could not find PythonClass entry" -