Open In App

Python | Sort Python Dictionaries by Key or Value

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

There are two elements in a Python dictionary-keys and values. You can sort the dictionary by keys, values, or both. In this article, we will discuss the methods of sorting dictionaries by key or value using Python.

Need for Sorting Dictionary in Python

We need sorting of data to reduce the complexity of the data and make queries faster and more efficient. Sorting is very important when we are dealing with a large amount of data. 

We can sort a dictionary by values using these methods:

  • First, sort the keys alphabetically using key_value.iterkeys() function.
  • Second, sort the keys alphabetically using the sorted (key_value) function & print the value corresponding to it.
  • Third, sort the values alphabetically using key_value.iteritems(), key = lambda (k, v) : (v, k))

Sort Python Dictionaries by Key or Value Examples

Here are the major tasks that need to be performed to sort a dictionary by value and keys in Python.

  1. Create a dictionary and display its list-keys alphabetically.
  2. Display both the keys and values, sorted by key in alphabetical order.
  3. At last, display both the keys and values, sorted by value in alphabetical order.

Example 1: Sorting Dictionary By Key

In this example, we will sort the dictionary by keys and the result type will be a dictionary. 

Python3




myDict = {'ravi': 10, 'rajnish': 9,
        'sanjeev': 15, 'yash': 2, 'suraj': 32}
 
myKeys = list(myDict.keys())
myKeys.sort()
sorted_dict = {i: myDict[i] for i in myKeys}
 
print(sorted_dict)


Output

{'rajnish': 9, 'ravi': 10, 'sanjeev': 15, 'suraj': 32, 'yash': 2}


Example 2: Displaying the Keys in Sorted Order

In this example, we are trying to sort the dictionary by keys and values in Python. Here, iterkeys() returns an iterator over the dictionary’s keys.

Python3




# Function calling
def dictionary():
    # Declare hash function
    key_value = {}
 
# Initializing value
    key_value[2] = 56
    key_value[1] = 2
    key_value[5] = 12
    key_value[4] = 24
    key_value[6] = 18
    key_value[3] = 323
 
    print("Task 1:-\n")
 
    print("key_value", key_value)
 
    # iterkeys() returns an iterator over the
    # dictionary’s keys.
    for i in sorted(key_value.keys()):
        print(i, end=" ")
 
 
def main():
    # function calling
    dictionary()
 
 
# Main function calling
if __name__ == "__main__":
    main()


Output

Task 1:-

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}
1 2 3 4 5 6 


Example 3: Sorting the dictionary by key 

In this example, we will sort in lexicographical order Taking the key’s type as a string.

Python3




# Creates a sorted dictionary (sorted by key)
from collections import OrderedDict
 
dict = {'ravi': '10', 'rajnish': '9',
        'sanjeev': '15', 'yash': '2', 'suraj': '32'}
dict1 = OrderedDict(sorted(dict.items()))
print(dict1)


Output

OrderedDict([('rajnish', '9'), ('ravi', '10'), ('sanjeev', '15'), ('suraj', '32'), ('yash', '2')])


Example 4: Sorting the Keys and Values Alphabetically Using the Key

In this example, we are trying to sort the dictionary by keys and values in Python. Here we are using an iterator over the Dictionary’s value to sort the keys.

Python3




# function calling
def dictionairy():
 
    # Declaring the hash function
    key_value = {}
 
# Initialize value
    key_value[2] = 56
    key_value[1] = 2
    key_value[5] = 12
    key_value[4] = 24
    key_value[6] = 18
    key_value[3] = 323
     
    print("key_value",key_value)
 
    print("Task 2:-\nKeys and Values sorted in",
          "alphabetical order by the key  ")
     
 
    # sorted(key_value) returns a sorted list
    # of the Dictionary’s keys.
    for i in sorted(key_value):
        print((i, key_value[i]), end=" ")
 
 
def main():
        # function calling
    dictionairy()
 
 
# main function calling
if __name__ == "__main__":
    main()


Output

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}
Task 2:-
Keys and Values sorted in alphabetical order by the key  
(1, 2) (2, 56) (3, 323) (4, 24) (5, 12) (6, 18) 


Example 5: Sorting the Keys and Values Alphabetically Using the Value

In this example, we are trying to sort the dictionary by keys and values in Python. Here we are using to sort in lexicographical order.

Python3




# Function calling
def dictionairy():
 
    # Declaring hash function
    key_value = {}
 
# Initializing the value
    key_value[2] = 56
    key_value[1] = 2
    key_value[5] = 12
    key_value[4] = 24
    key_value[6] = 18
    key_value[3] = 323
     
    print("key_value",key_value)
 
    print("Task 3:-\nKeys and Values sorted",
          "in alphabetical order by the value")
 
    # Note that it will sort in lexicographical order
    # For mathematical way, change it to float
    print(sorted(key_value.items(), key=lambda kv:
                 (kv[1], kv[0])))
 
 
def main():
    # function calling
    dictionairy()
 
 
# main function calling
if __name__ == "__main__":
    main()


Output

key_value {2: 56, 1: 2, 5: 12, 4: 24, 6: 18, 3: 323}
Task 3:-
Keys and Values sorted in alphabetical order by the value
[(1, 2), (5, 12), (6, 18), (4, 24), (2, 56), (3, 323)]


The time complexity for this program is O(n log n), where n is the number of key-value pairs in the dictionary.

The auxiliary space complexity for this program is also O(n), where n is the number of key-value pairs in the dictionary. 

Example 6: Sorting Dictionary By Value 

In this example, we are trying to sort the dictionary by values in Python. Here we are using dictionary comprehension to sort our values.

Python3




# Creates a sorted dictionary (sorted by key)
from collections import OrderedDict
import numpy as np
 
dict = {'ravi': 10, 'rajnish': 9,
        'sanjeev': 15, 'yash': 2, 'suraj': 32}
print(dict)
 
keys = list(dict.keys())
values = list(dict.values())
sorted_value_index = np.argsort(values)
sorted_dict = {keys[i]: values[i] for i in sorted_value_index}
 
print(sorted_dict)


Output:

{'ravi': 10, 'rajnish': 9, 'sanjeev': 15, 'yash': 2, 'suraj': 32}
{'ravi': 2, 'rajnish': 9, 'sanjeev': 10, 'yash': 15, 'suraj': 32}

Time complexity: O(n log n), where n is the number of items in the dictionary.
Auxiliary space: O(n), as we are creating new lists of keys and values, and creating a new dictionary with the same number of entries as the original dictionary.

We have covered different examples based on sorting dictionary by key or value. Reading and practicing these Python codes will help you understand sorting in Python dictionaries.

You can easily sort the values of dictionaries by their key or value.

Similar Reads:



Last Updated : 21 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads