Sorting Dictionary by Key Order

  • Share this:

Code introduction


The function accepts a dictionary as an argument and returns a new dictionary. The new dictionary has key-value pairs sorted by dictionary order of the keys.


Technology Stack : Standard library data types and dictionaries

Code Type : Function

Code Difficulty : Intermediate


                
                    
def aordict(d):
    return {k: aordict(v) if isinstance(v, dict) else v for k, v in sorted(d.items())}