Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8543545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:19:39+00:00 2026-06-11T12:19:39+00:00

Suppose I have a dictionary in python and I sort the keys by values

  • 0

Suppose I have a dictionary in python and I sort the keys by values as follows

my_dict = {'a':5, 'b':4, 'c':6, 'd':3, 'e':2}
sorted_list = sorted(my_dict, key=my_dict.get)
sorted_list
['e', 'd', 'b', 'a', 'c']

This works fine, but if I have collisions in my dictionary (with respect to values)

my_dict = {'a':5, 'b':4, 'c':5, 'd':3, 'e':2, 'f':4, 'g':3}
sorted_list = sorted(my_dict, key=my_dict.get)
sorted_list
['e', 'd', 'g', 'b', 'f', 'a', 'c']

It seems to me that in case of duplicates, the list is sorted by its order(numerical order or insertion order, not the sorted order) in the dictionary. Can I assume it to be true for all cases?

EDIT1: My keys will be integers and in case the values collide, I want the resulting keys to be sorted by their values(highest first)

EDIT2: Adding the following parameter helps my question. But, in my case, I want the values to be sorted in ascending, but the keys to be sorted in descending. THe following will result in both to descending
key=lambda x: (x[1],x[0])

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:19:41+00:00Added an answer on June 11, 2026 at 12:19 pm

    Python’s sort algorithm (TimSort) is a stable sort, so any items that have the same sort ‘value’, are kept in the order they were in before sorting.

    When sorting dictionary keys, that means they are kept in the same order as returned by dict.keys() or when iterating over the dictionary. Do note that this ordering is arbitrary, and not stable across changes to the dictionary.

    Otherwise yes, you can assume that to be true for all cases.

    As pointed out by Steven Rumbalski, you can take the key along with the sorting to stabilize same-value sorting ordering:

    sorted_list = sorted(my_dict, key=lambda k: (my_dict[k], k))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have Dictionary like this: Dictionary<string, string> values = new Dictionary<string, string>() {
Suppose I have a Dictionary like so: Dictionary<string, Stream> How can I get a
I have a dictionary d (and a seperate sorted list of keys, keys ).
suppose I have a dictionary whose keys are strings. How can I efficiently make
Suppose I have a method to return a list of keys in a dictionary,
Let's suppose I have a Winforms app with a Dictionary that looks like this:
Suppose I have a Dictionary<string, string> instance, like this one: { { a, 1
i am implementing dictionary in which key is a string keyword.suppose i have following
I was asked this question in an interview. Suppose you have an ordered dictionary,
Suppose I have a dictionary, and it's nested with dictionaries inside. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.