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 8544491
In Process

The Archive Base Latest Questions

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

I want to get a sorted list of keys from a dictionary in python

  • 0

I want to get a sorted list of keys from a dictionary in python (My dictionary is of int to int mapping). I want the values to be sorted in ascending. But in case of collision (same value), I want the keys to be sorted by the numeric value of the key. The following piece of code does that

key=lambda x: (dict[x],x)

However, I want the ordering of the values in descending and ordering of the keys (only in case of collision) to be descending. I can apply the reverse parameter, but that will work on both key and its value in the same way.

Is there any way to do it separately?

Example

>>> my_dict = {1:5, 2:4, 3:5, 26:3, 5:2, 6:4, 8:3}
>>> my_dict
{1: 5, 2: 4, 3: 5, 5: 2, 6: 4, 8: 3, 26: 3}
>>> sorted_list = sorted(my_dict, key=my_dict.get)
>>> print sorted_list
[5, 8, 26, 2, 6, 1, 3]

I want the output to be
[5, 26, 8, 6, 2, 3, 1]

  • 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:34:18+00:00Added an answer on June 11, 2026 at 12:34 pm

    You have a few options:

    >>> my_dict = {1:5, 2:4, 3:5, 26:3, 5:2, 6:4, 8:3}
    >>> sorted(my_dict, key=lambda x: (my_dict[x], -x))
    [5, 26, 8, 6, 2, 3, 1]
    >>> sorted(sorted(my_dict)[::-1], key=my_dict.get)
    [5, 26, 8, 6, 2, 3, 1]
    >>> sorted(sorted(my_dict, reverse=True), key=my_dict.get)
    [5, 26, 8, 6, 2, 3, 1]
    

    The first depends upon the keys being numerical (or at least something which you can negate like this), which is less general than the last two, which use only the fact that the sort is stable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a dictionary in python and I sort the keys by values
I want to print out a dictionary, sorted by the key. Sorting the keys
I want to get the elements sorted by the number of their occurence. This
I want get the time used for a case so I can create an
I want get as much as possible from Redis + Hiredis + libevent. I'm
What I want: get a xml from the AppData to use What I code
Basically I want get data I already have accessed from javascript and passing it
I just want get a 2 dimension array of List in c#. In my
I want to get list of tables in a given database and a given
I want to get 100 and example from this string ?connect:100/username:example/ I searched in

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.