Reputation: 109
I'm trying to get the fruit()
function to sort using key=sortby()
and then print to screen - either within fruit()
or within it's own function.
It works fine when there is no fruit()
function, but I'm having difficulties working out the right syntax to pass par
as a parameter to be used in fruit()
fruit = [["Apples", 30], ["Bananas", 100], ["Pears", 0], ["Peaches", 20]]
def sortby(par):
return par[1]
def fruit():
rate = []
fruit.sort(key=sortby, reverse=True)
for success in fruit:
rate.append(success[0])
rate.append(success[1])
str = str(rate)
print(str)
Upvotes: 2
Views: 683
Reputation: 9858
There are several issues with your code:
fruit
functionfruit
function a different name from the listfruit
function at some pointstr
as a variable name, as this is a built-in function in Python.The sortby
function works fine, however.
fruit = [["Apples", 30], ["Bananas", 100], ["Pears", 0], ["Peaches", 20]]
def sortby(par):
return par[1]
def sort_fruit():
rate = []
fruit.sort(key=sortby, reverse=True)
for success in fruit:
rate.append(success[0])
rate.append(success[1])
return rate
print(sort_fruit())
Upvotes: 4