Finding max value in the second column of a nested list?

42,307

Solution 1

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.

Solution 2

Use the key argument.

max(alk..., key=operator.itemgetter(1))
Share:
42,307
davelupt
Author by

davelupt

Updated on January 03, 2022

Comments

  • davelupt
    davelupt over 2 years

    I have a list like this:

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

    If I simply use the max(list) method, it will return the answer 'strontium', which would be correct if I was trying to find the max name, however I'm trying to return the element whose integer is highest.

  • davelupt
    davelupt over 13 years
    Would you mind explaining in laymen's terms what this is doing. I think I understand that the x: x[1] defines the position, because if I substitute in 0 for 1 it returns the correct max string value. But, however I do not completely understand the point of key and lambda despite looking at the Python library.
  • Spike Gronim
    Spike Gronim over 13 years
    key is a keyword argument to the max() builtin function. The key argument should be a callable (function, lambda, etc.) that given an element of the iterable returns a value for use in comparison. The "lambda x: x[1]" creates an anonymous function (lambda) that returns the second element of something that supports the Python slice notation.
  • davelupt
    davelupt over 13 years
    So if I had another level of nesting in a list like alkaline_earth_values = [['beryllium', 4,['a' ,1]],['magnesium', 12, ['b',2]],['calcium', 20,['c', 3]],['strontium', 38 ['d', 4]],['barium', 56['e', 5]], ['radium', 88['f', 1]]] Would I do max(alkaline_earth_metals, key = lambda x:x[4] in order to sort by the triple nested integer?
  • kynnysmatto
    kynnysmatto over 13 years
    You mean by the values 1,2,3,4,5,1? In that case you would do: key=lambda x: x[2][1]. Because you would first be taking the 2nd element of ['beryllium', 4, [a, 1]] which would be [a, 1]. And then you would continue by taking the 1st element of that list, which would be 1.
  • davelupt
    davelupt over 13 years
    or would it be print(max(alkaline_earth_metals, key=lambda x: x[2][1]))
  • davelupt
    davelupt over 13 years
    Thank you very much, I think I get it now.
  • dbader
    dbader almost 8 years
    I've been working with a team who only recently switched to Python and I saw how easy it is to get confused by using max() with nested sequences. I ended up writing a tutorial to do a bit of a deep dive into how the default sorting behavior can be modified, maybe this helps someone out: dbader.org/blog/python-min-max-and-nested-lists
  • user977828
    user977828 over 3 years
    If multiple lists have the same value as an element. How would lambda or itemgetter get them?
  • user977828
    user977828 over 3 years
    If multiple lists have the same value as an element. How would lambda print the duplications out?
  • user977828
    user977828 over 3 years
    If multiple lists have the same value as an element. How would itemgetter print the duplication out?
  • user977828
    user977828 over 3 years
    If multiple lists have the same value as an element. How would pandas print the duplication out?
  • Chris_Rands
    Chris_Rands over 2 years
    @user977828 it requires a second iteration, e.g. m = max(alkaline_earth_values, key=operator.itemgetter(1)); [item for item in alkaline_earth_values if item[1] == m[1]]