-2

How can I order the values for my dictionary?

            sorted_activities = sorted(activity_dicc.items(),reverse=True)
            for activity,duration in sorted_activities :
                    print(activity,duration)

activity_dicc is my dictionary, activity is the key and duration is the value.

When I run my code, it is not sorted. I hoped for it to appear in order by the duration going from highest to lowest.

(The code is way too long to show, but that's the part with the issue. I need to set up a place so it knows how to sort it by, but I'm not sure where to do that.)

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
tris
  • 1

1 Answers1

-1

You cannot sort dictionaries. By definition they are unsorted key-value pairs. You should create a sorted list with the dictionary keys and run a loop to print the dictionary values that match the keys.

sorted_activities = sorted(activity_dicc.keys(), reverse=True)
for activity in sorted_activities:
    print(activity_dicc[activity])
Tyler Sims
  • 24
  • 2