You have a dictionary of student names and their scores. How would you get a list of student names sorted by their scores in descending order?

  • sorted_names = sorted(students.items(), key=lambda x: x[1])
  • sorted_names = sorted(students.items(), key=lambda x: x[1], reverse=True)
  • sorted_names = students.items().sort(key=lambda x: x[1])
  • sorted_names = students.items().sort(key=lambda x: x[1], reverse=True)
The correct way to sort the dictionary by scores in descending order is to use the sorted() function with a lambda function as the key argument. The lambda function extracts the score (value) from each (key, value) pair and sorts the items accordingly.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *