Sorting Dictionary by Value in Python

  • Share this:

Code introduction


This function sorts the dictionary items by their values and returns a new dictionary with the items ordered by the sorted values.


Technology Stack : Built-in function sorted, lambda expressions, tuple unpacking

Code Type : Sort function

Code Difficulty : Intermediate


                
                    
def sort_dict_by_value(d):
    return dict(sorted(d.items(), key=lambda item: item[1]))