Python | Sort Python Dictionaries by Key or Value

Created with Sketch.

Python | Sort Python Dictionaries by Key or Value

Problem Statement – Here are the major tasks that are needed to be performed.

  1. Create a dictionary and display its keys alphabetically.
  2. Display both the keys and values sorted in alphabetical order by the key.
  3. Same as part (ii), but sorted in alphabetical order by the value.

Approach –
Load the Dictionary and perform the following operations:

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

Let’s try performing the above-mentioned tasks:

  1. Displaying the Keys Alphabetically:Examples:
    Input:
    key_value[2] = '64'       
    key_value[1] = '69'
    key_value[4] = '23'
    key_value[5] = '65'
    key_value[6] = '34'
    key_value[3] = '76'
    
    Output:
    1 2 3 4 5 6 
    

    Program:

    # Function calling
    def dictionairy(): 
     # 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 ("Keys are")
      
     # iterkeys() returns an iterator over the 
     # dictionary’s keys.
     for i in sorted (key_value.keys()) : 
         print(i, end = " ")
     
    def main():
        # function calling
        dictionairy()             
         
    # Main function calling
    if __name__=="__main__":      
        main()
    Output:

    Task 1:-
    
    Keys are
    1 2 3 4 5 6
    
  2. Sorting the Keys and Values in Alphabetical Order using the Key.Examples:
    Input:
    key_value[2] = '64'       
    key_value[1] = '69'
    key_value[4] = '23'
    key_value[5] = '65'
    key_value[6] = '34'
    key_value[3] = '76'
    
    Output:
    (1, 69) (2, 64) (3, 76) (4, 23) (5, 65) (6, 34)
    

    Program:

    # 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 ("Task 2:-\nKeys and Values sorted in"
                "alphabetical order by the key  ")  
     
     # sorted(key_value) returns an iterator over the 
     # Dictionary’s value sorted in 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:

    Task 2:-
    Keys and Values sorted in alphabetical order by the key  
    (1, 2) (2, 56) (3, 323) (4, 24) (5, 12) (6, 18)
    
  3. Sorting the Keys and Values in alphabetical using the valueExamples:
    Input:
    key_value[2] = '64'       
    key_value[1] = '69'
    key_value[4] = '23'
    key_value[5] = '65'
    key_value[6] = '34'
    key_value[3] = '76'
    
    Output:
    (4, 23), (6, 34), (2, 64), (5, 65), (1, 69), (3, 76)
    

    Program:

    # 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 ("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:

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

Learning Outcome:

  • How to handle a dictionary.
  • Dictionary has O(1) search time complexity whereas List has O(n) time complexity. So it is recommended to use the dictionary where ever possible.

Leave a Reply

Your email address will not be published. Required fields are marked *