Cute
0
Q:

write a program to sort python ditionaries by key or value

newlist = sorted(list_to_be_sorted, key=lambda k: k['name']) 
1
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
{k: v for k, v in sorted(x.items(), key=lambda item: item[1])}
{0: 0, 2: 1, 1: 2, 4: 3, 3: 4}
6
# 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() 
0

New to Communities?

Join the community