Finding max value in the second column of a nested list?
max(alkaline_earth_values, key=lambda x: x[1])
The reason this works is because the key argument of the max function specifies a function that is called when max wants to know the value by which the maximum element will be searched. max will call that function for each element in the sequence. And lambda x: x[1]
creates a small function which takes in a list and returns the first (counting starts from zero) element. So
k = lambda x: x[1]
is the same as saying
def k(l):
return l[1]
but shorter and nice to use in situations like this.
Use the key
argument.
max(alk..., key=operator.itemgetter(1))