Python program to sort Dictionary by Key Lengths
Last Updated :
02 May, 2023
Given Dictionary, sort by its key lengths.
Input : test_dict = {“Gfg” : 4, “is” : 1, “best” : 0, “for” : 3, “geeks” : 3}
Output : {‘is’: 1, ‘Gfg’: 4, ‘for’: 3, ‘best’: 0, ‘geeks’: 3}
Explanation : 2 < 3 = 3 < 4 < 5, are sorted lengths in order.
Input : test_dict = {“Gfg” : 4, “for” : 3, “geeks” : 3}
Output : {‘Gfg’: 4, ‘for’: 3, ‘geeks’: 3}
Explanation : 3 = 3 < 5, are sorted lengths in order.
Method #1 : Using len() + sort() + dictionary comprehension + items()
In this, we perform the task of sorting using sort(), items() is used to get tuple pair from the dictionary, len() gets the keys lengths. Then dictionary comprehension performs the task of converting back to dictionary.
Python3
def get_len(key):
return len (key[ 0 ])
test_dict = { "Gfg" : 4 , "is" : 1 , "best" : 0 , "for" : 3 , "geeks" : 3 }
print ( "The original dictionary is : " + str (test_dict))
test_dict_list = list (test_dict.items())
test_dict_list.sort(key = get_len)
res = {ele[ 0 ] : ele[ 1 ] for ele in test_dict_list}
print ( "The sorted dictionary : " + str (res))
|
Output:
The original dictionary is : {‘Gfg’: 4, ‘is’: 1, ‘best’: 0, ‘for’: 3, ‘geeks’: 3} The sorted dictionary : {‘is’: 1, ‘Gfg’: 4, ‘for’: 3, ‘best’: 0, ‘geeks’: 3}
Time complexity: O(n*logn)
Space complexity: O(1)
Method #2 : Using sorted() + lambda function + items() + dictionary comprehension
In this, we perform the task of sorting using sorted(), lambda function is used to provide the logic of getting key lengths.
Python3
test_dict = { "Gfg" : 4 , "is" : 1 , "best" : 0 , "for" : 3 , "geeks" : 3 }
print ( "The original dictionary is : " + str (test_dict))
test_dict_list = sorted ( list (test_dict.items()), key = lambda key : len (key[ 0 ]))
res = {ele[ 0 ] : ele[ 1 ] for ele in test_dict_list}
print ( "The sorted dictionary : " + str (res))
|
Output:
The original dictionary is : {‘Gfg’: 4, ‘is’: 1, ‘best’: 0, ‘for’: 3, ‘geeks’: 3} The sorted dictionary : {‘is’: 1, ‘Gfg’: 4, ‘for’: 3, ‘best’: 0, ‘geeks’: 3}
Method #5: Using zip() + sorted() + dictionary constructor
First, a dictionary called test_dict is initialized with 5 key-value pairs, where each key is a string and each value is an integer.
The original dictionary is printed using the print() function and the concatenation operator + to join the string “The original dictionary is : ” and the string representation of the test_dict dictionary obtained using the str() function.
The sorted() function is used to sort the keys of the test_dict dictionary by their length in ascending order. The sorted keys are assigned to the variable sorted_keys.
The zip() function is used to create a new iterable where each element is a tuple consisting of a key from the sorted_keys list and the corresponding value from the test_dict dictionary.
The list comprehension [test_dict[key] for key in sorted_keys] is used to create a list of values corresponding to the keys in the sorted_keys list, in the order specified by sorted_keys.
The dict() function is used to create a new dictionary from the tuples generated by the zip() function. This new dictionary is assigned to the variable sorted_dict.
The sorted dictionary is printed using the print() function and the concatenation operator + to join the string “The sorted dictionary : ” and the string representation of the sorted_dict dictionary obtained using the str() function.
Python3
test_dict = { "Gfg" : 4 , "is" : 1 , "best" : 0 , "for" : 3 , "geeks" : 3 }
print ( "The original dictionary is : " + str (test_dict))
sorted_keys = sorted (test_dict.keys(), key = len )
sorted_dict = dict ( zip (sorted_keys, [test_dict[key] for key in sorted_keys]))
print ( "The sorted dictionary : " + str (sorted_dict))
|
Output
The original dictionary is : {'Gfg': 4, 'is': 1, 'best': 0, 'for': 3, 'geeks': 3}
The sorted dictionary : {'is': 1, 'Gfg': 4, 'for': 3, 'best': 0, 'geeks': 3}
Time complexity: O(n*log(n)), where n is the number of items in the dictionary.
Auxiliary space: O(n), where n is the number of items in the dictionary.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...