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

  • SEARCH
  • Home
  • 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 8399629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:21:22+00:00 2026-06-09T21:21:22+00:00

So I have a dictionary that looks like this when I print it: {’10’:

  • 0

So I have a dictionary that looks like this when I print it:

{'10': -10, 'ZT21': 14, 'WX21': 12, '2': 15, '5': -3, 'UM': -25}

I want to sort these in a custom manner, which I define. Let’s say the way I want it to be sorted (by key) is ZT21, 10, WX21, UM, 5, 2.

Anyone know how to go about sorting out a dictionary in a predefined/custom manner? What I am doing is getting this dictionary from a database, and it can come out with over 20 keys, all of which have a specific order. The order is always set, but sometimes certain keys/values wouldn’t be in the dictionary. So this could happen too:

{'ZT21': 14, 'WX21': 12, '2': 15, '5': -3, 'UM': -25}

sorted (by key) is ZT21, 10, WX21, UM, 5, 2.

So the 10 isn’t there in this example, but the sorting I need is still the same, the 10 would just be absent.

Any ideas?

  • 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-09T21:21:23+00:00Added an answer on June 9, 2026 at 9:21 pm

    Updated answer for Python 3.6+

    >>> d = {'10': -10, 'ZT21': 14, 'WX21': 12, '2': 15, '5': -3, 'UM': -25}
    >>> keyorder = ['ZT21', '10', 'WX21', 'UM', '5', '2']
    >>> {k: d[k] for k in keyorder if k in d}
    {'ZT21': 14, '10': -10, 'WX21': 12, 'UM': -25, '5': -3, '2': 15}
    

    Legacy answer:
    Dictionaries in Python are unordered (before Python3.6). You can get the results you need as a list

    >>> d = {'10': -10, 'ZT21': 14, 'WX21': 12, '2': 15, '5': -3, 'UM': -25}
    >>> keyorder = ['ZT21', '10', 'WX21', 'UM', '5', '2']
    >>> sorted(d.items(), key=lambda i:keyorder.index(i[0]))
    [('ZT21', 14), ('10', -10), ('WX21', 12), ('UM', -25), ('5', -3), ('2', 15)]
    

    or as an OrderedDict

    >>> from collections import OrderedDict
    >>> OrderedDict(sorted(d.items(), key=lambda i:keyorder.index(i[0])))
    OrderedDict([('ZT21', 14), ('10', -10), ('WX21', 12), ('UM', -25), ('5', -3), ('2', 15)])
    

    If you are doing a lot of these, it will be more efficient to use a dict for the keyorder

    >>> keyorder = {k:v for v,k in enumerate(['ZT21', '10', 'WX21', 'UM', '5', '2'])}
    >>> OrderedDict(sorted(d.items(), key=lambda i:keyorder.get(i[0])))
    OrderedDict([('ZT21', 14), ('10', -10), ('WX21', 12), ('UM', -25), ('5', -3), ('2', 15)])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary that looks like this MyCount= {u'10': 1, u'1': 2, u'3':
I have a dictionary that looks something like this test1 : 123 test2 :
Let's suppose I have a Winforms app with a Dictionary that looks like this:
I have a dictionary that looks like this Dictionary<string,string[]> I also have 2 dropdownlist.
I have a dictionary that looks like this: diction = {1: {'Type': 'Cartoon', 'Time':
I have a dictionary that looks like this: myDict = { 'SER12346': {'serial_num': 'SER12346',
I have a dictionary where I want to alphabetically sort the keys that have
Say I have a string that looks like this: str = The &yquick &cbrown
I have a piece of code that looks like this: ipCount = defaultdict(int) for
I have a csv file that looks like this: Please note, there are no

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.