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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:24:06+00:00 2026-05-12T16:24:06+00:00

I have dict in Python with keys of the following form: mydict = {‘0’

  • 0

I have dict in Python with keys of the following form:

mydict = {'0'     : 10,
          '1'     : 23,
          '2.0'   : 321,
          '2.1'   : 3231,
          '3'     : 3,
          '4.0.0' : 1,
          '4.0.1' : 10,
          '5'     : 11,
          # ... etc
          '10'    : 32,
          '11.0'  : 3,
          '11.1'  : 243,
          '12.0'  : 3,
          '12.1.0': 1,
          '12.1.1': 2,
          }

Some of the indices have no sub-values, some have one level of sub-values and some have two. If I only had one sub-level I could treat them all as numbers and sort numerically. The second sub-level forces me to handle them all as strings. However, if I sort them like strings I’ll have 10 following 1 and 20 following 2.

How can I sort the indices correctly?

Note: What I really want to do is print out the dict sorted by index. If there’s a better way to do it than sorting it somehow that’s fine with me.

  • 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-05-12T16:24:06+00:00Added an answer on May 12, 2026 at 4:24 pm

    You can sort the keys the way that you want, by splitting them on ‘.’ and then converting each of the components into an integer, like this:

    sorted(mydict.keys(), key=lambda a:map(int,a.split('.')))
    

    which returns this:

    ['0',
     '1',
     '2.0',
     '2.1',
     '3',
     '4.0.0',
     '4.0.1',
     '5',
     '10',
     '11.0',
     '11.1',
     '12.0',
     '12.1.0',
     '12.1.1']
    

    You can iterate over that list of keys, and pull the values out of your dictionary as needed.

    You could also sort the result of mydict.items(), very similarly:

    sorted(mydict.items(), key=lambda a:map(int,a[0].split('.')))
    

    This gives you a sorted list of (key, value) pairs, like this:

    [('0', 10),
     ('1', 23),
     ('2.0', 321),
     ('2.1', 3231),
     ('3', 3),
     # ...
     ('12.1.1', 2)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary in Python where the keys are pathnames. For example: dict[/A]
I have a Python datetime timestamp and a large dict (index) where keys are
Possible Duplicate: python dict.add_by_value(dict_2) ? My input is two dictionaries that have string keys
I have a python dict as below mymap={'java':40,'haskell':60,'ruby':50,'python':70,'scala':30,'lisp':80,'scheme':75} I have the keys put in
Have dict like: mydict= {'a':[],'b':[],'c':[],'d':[]} list like: log = [['a',917],['b', 312],['c',303],['d',212],['a',215],['b',212].['c',213],['d',202]] How do i
My issue is as follows. We have dict that has all keys and values
I have a dict which contains some lists and some dicts, as illustrated below.
I have the following Python 2.7 dictionary data structure (I do not control source
I have the following code. class person(object): def __init__(self, keys): for item in keys:
In python 2.6, I defined a class Abc, made a dict d where keys

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.