What is the proper way to print a nested list with the highest value in Python
How about:
print(max(x, key=sum))
Demo:
>>> x = [[1,2,3],[4,5,6],[7,8,9]]>>> print(max(x, key=sum))[7, 8, 9]
This works because max
(along with a number of other python builtins like min
, sort
...) accepts a function to be used for the comparison. In this case, I just said that we should compare the elements in x
based on their individual sum
and Bob's our uncle, we're done!