wrong output in sorting a list of list in python

Question:

I have this sample list of list:

[[0.7578684, 'Yes, import function which lets you load items'],[1.7032554, 'Use the Test tools to edit'], [0.58279467, 'Yes, use the Designer UI Import feature to restore content from the JSON file.']]

I want to sort this list in descending order based on the integer value in each list. I wrote these lines but I am getting wrong output:

sorted_answer = answer.sort(key=lambda x: float(x[0]),reverse = True)
print(sorted_answer)

When I execute this I get None as output for sorted_answer list. What is the mistake I am making?

Asked By: user2916886

||

Answers:

you in place sort the list, sort return None. if you want sort return function, use sorted

answer.sort(key=lambda x: float(x[0]),reverse = True)
print(answer)

This return None and po assign is back to answer, thus the answer is None . The sort function is in place, no need to assign back

Answered By: galaxyan
sorted(answer, reverse=True)

gives

[[1.7032554, 'Use the Test tools to edit'], 
[0.7578684, 'Yes, import function which lets you load items'], 
[0.58279467, 'Yes, use the Designer UI Import feature to restore content from the JSON file.']]
Answered By: Omri Bahat Treidel
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.