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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:20:36+00:00 2026-06-11T02:20:36+00:00

I am working on a dict in python. I am trying to sort it

  • 0

I am working on a dict in python. I am trying to sort it out alphabetically and split it to look slightly better.
Here is the code I have so far in the dictonatary.

authorentry = {'author':  name, 'date': datef , 'path': path_change , 'msg' : xmlMsgf }           
if not name in author:
    author[ name ] = []

author[ name ].append( authorentry )       

if not authorentry in author.items():
    author['author'] = [authorentry]

print sorted (author.keys()), sorted (author.values())

Now what i want it to do is print out the dict in a sorted order based on the author and the date. and also to split it and modified it so it doesn’t have all those commas and ‘u’s if that is possible. Any ideas on how to accomplish it?

this is what it looks like when i print it it as it is.

how i want is author shows up first in the list not date. And I would like it alphabetical if possible and to remove the commas in the entrys to print it out cleaner. is it possible?

[[{'date': ['06-08-2012 09:01:52 PM'], 'path': [u'/branches/Patch_4_2_0_Branch'], 'msg': ['none', u'PATCH_BRANCH:N/A\nBUG_NUMBER:N/A\nFEATURE_AFFECTED:N/A\nOVERVIEW:N/A\nAdding the SVN log size requirement to the branch \n'], 'author': u'glv'}], [{'date': ['06-08-2012 09:01:52 PM'], 'path': [u'/branches/Patch_4_2_0_Branch'], 'msg': ['none', u'PATCH_BRANCH:N/A\nBUG_NUMBER:N/A\nFEATURE_AFFECTED:N/A\nOVERVIEW:N/A\nAdding the SVN log size requirement to the branch \n'], 'author': u'glv'}]]

Update: as of right now I can group the authors together but for some reason not only can’t i get it alphebetized I cannot even get the author to be the first person on the list what shows up is something similar to this:

 Date: 06-08-2012 08:56:09 PM

  Changes by : glv

  Comments: PATCH_BRANCH:N/A BUG_NUMBER:N/A FEATURE_AFFECTED:N/A OVERVIEW:N/A Adding the svn commit line requrement  

            Directory Location: /trunk

The way i wanted it ordered is more like this.

  Changes by : glv
  Date: 06-08-2012 08:56:09 PM
  Directory Location: /trunk
  Comments: PATCH_BRANCH:N/A BUG_NUMBER:N/A FEATURE_AFFECTED:N/A OVERVIEW:N/A Adding the svn commit line requrement

I tried the OrderedList to see if I can get it to work that way but so far no luck or success. Is there something I am missing?

  • 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-11T02:20:38+00:00Added an answer on June 11, 2026 at 2:20 am

    If you just care about presenting this information for user readability, use pprint module.

    import pprint
    pprint.pprint(author)
    

    assuming author is a dict. Alternatively use pprint.pformat to get a string which you can further manipulate/clean up e.g. print pprint.pformat(author).replace(',','') to remove commas.

    You should also know that dicts can not be reordered since they are essentially a hashtable whos keys are hashes (like a set).

    You can also try using collections.OrdererdDict:

    from collections import OrdererdDict
    sorted_author = OrderedDict(sorted(author.iteritems()))
    

    Update: its strange you are still having problems with this. Ill just give you some code that will definitely work and you can adapt it from there:

    def format_author(author):
        tups = sorted(author.iteritems())           # alphabetical sorting
        kmaxlen = max([len(k) for k, v in tups])    # for output alignment
    
        # some custom rearrangement. if there is a 'msg' key, we want it last
        tupkeys = [k for k, v in tups]
        if 'msg' in tupkeys:
            msg_tup = tups.pop(tupkeys.index('msg'))
            tups.append(msg_tup)    # append to the end
            # alternatively tups.insert(0, msg_tup) would insert at front
    
        output = []
    
        for k, v in tups:
            # dress our values
            if not v:
                v = ''
            elif isinstance(v, list):
                if len(v) == 1:
                    v = v[0]
                if len(v) == 2 and v[0] in [None, 'none', 'None']:
                    v = v[1]
             v = v.strip()
            output.append("%s: %s" % (k.rjust(kmaxlen), v))
        return "\n".join(output)
    

    Then you can do something like:

    author = {'date': ['06-08-2012 09:01:52 PM'], 'path': [u'/branches/Patch_4_2_0_Branch'], 'author': u'glv', 'msg': ['none', u'blah blah blah \n']}
    s = format_author(author)
    print s
    

    and get output like this:

    author: glv
      date: 06-08-2012 09:01:52 PM
      path: /branches/Patch_4_2_0_Branch
       msg: blah blah blah
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a handle on multithreading in python. I have working code
I'm trying to simulate some code that I have working with SQL but using
I have this piece of code that is working fine in python 2.7. dist
now I am working with python. So one question about dict .... suppose I
Working with Reporting Services 2008 r2. So here's my issue: We have 5 reports
I am working on an application in Python/Django. I am trying to make a
I'm trying to implement dynamic reloading objects in Python, that reflect code changes live.
I have setup an Apache2 mod_python environment with stackless Python and it is working.
working on creating an XML file with some data using Python. I am trying
I have a lot of archive data in python dict format that I am

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.