I have a dictionary like this
({'Apple': [1,5,3,1,6], 'Banana': [9,12,4,5,1]})
and I want to create a sorted list out of it where sorting is based on the sum of the values.
The following code does not work though:
sorted_list = sorted(mylist.items(), key=lambda x: sum(x[1]), reverse=True)
It always says TypeError: 'int' object is not callable
.
If I leave out the sum
function it works, but sorting is based on the FIRST value in the list. If I try something like key=lambda x: (x[1][0]+x[1][1])
it sums up the first two values in the list and sorts based on the result. So sorting based on the sum DOES WORK, but not with the sum
function but only by manually adding the elements. Why though?