Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython – Custom order dictionary

Python – Custom order dictionary

Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform the custom ordering of keys of dictionary. This is quite popular problem, with the advent of newer version of Python, where keys are ordered in Dictionaries, there might be requirement to reorder dictionary keys. Let’s discuss certain ways in which this task can be performed.

Input : test_dict = {‘gfg’: 1, ‘is’: 2, ‘best’: 3, ‘for’: 4, ‘Lazyroar’: 5} Output : {‘gfg’: 1, ‘is’: 2, ‘best’: 3, ‘for’: 4, ‘Lazyroar’: 5} Input : test_dict = {‘Lazyroar’: 5, ‘for’: 4, ‘best’: 3, ‘is’: 2, ‘gfg’: 1} Output : {‘gfg’: 1, ‘is’: 2, ‘best’: 3, ‘for’: 4, ‘Lazyroar’: 5}

Method #1 : Using loop This is brute force way to solve this problem. In this, we construct the newer dictionary by appending the keys in order list, mapping with keys in original dictionary. Works with Python >= 3.6. 

Python3




# Python3 code to demonstrate working of
# Custom order dictionary
# Using loop
 
# initializing dictionary
test_dict = {'is' : 2, 'for' : 4, 'gfg' : 1, 'best' : 3, 'Lazyroar' : 5}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing order
ord_list = ['gfg', 'is', 'best', 'for', 'Lazyroar']
 
# Custom order dictionary
# Using loop
res = dict()
for key in ord_list:
    res[key] = test_dict[key]
 
# printing result
print("Ordered dictionary is : " + str(res))


Output : 

The original dictionary is : {‘is’: 2, ‘for’: 4, ‘gfg’: 1, ‘best’: 3, ‘Lazyroar’: 5} Ordered dictionary is : {‘gfg’: 1, ‘is’: 2, ‘best’: 3, ‘for’: 4, ‘Lazyroar’: 5}

  Method #2 : Using dictionary comprehension This is yet another way in which this task can be performed. In this, we use shorthand to solve the problem using the same above method. 

Python3




# Python3 code to demonstrate working of
# Custom order dictionary
# Using dictionary comprehension
 
# initializing dictionary
test_dict = {'is' : 2, 'for' : 4, 'gfg' : 1, 'best' : 3, 'Lazyroar' : 5}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing order
ord_list = ['gfg', 'is', 'best', 'for', 'Lazyroar']
 
# Custom order dictionary
# Using dictionary comprehension
res = {key : test_dict[key] for key in ord_list}
 
# printing result
print("Ordered dictionary is : " + str(res))


Output : 

The original dictionary is : {‘is’: 2, ‘for’: 4, ‘gfg’: 1, ‘best’: 3, ‘Lazyroar’: 5} Ordered dictionary is : {‘gfg’: 1, ‘is’: 2, ‘best’: 3, ‘for’: 4, ‘Lazyroar’: 5}

Using a list comprehension and the min function:

Approach:

We can use a list comprehension to generate a list of tuples containing the key-value pairs, find the minimum key using the min function, and then create a new dictionary by iterating over the keys in sorted order. 

Python3




def order_dict_4(input_dict):
    pairs = [(k, v) for k, v in input_dict.items()]
    min_key = min(pairs, key=lambda x: x[0])[0]
    return {k: v for k, v in sorted(pairs, key=lambda x: x[0]-min_key)}
test_dict = {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'Lazyroar': 5}
print(test_dict)
test_dict = {'Lazyroar': 5, 'for': 4, 'best': 3, 'is': 2, 'gfg': 1}
print(test_dict)


Output

{'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'Lazyroar': 5}
{'Lazyroar': 5, 'for': 4, 'best': 3, 'is': 2, 'gfg': 1}

Time complexity: O(n log n), where n is the number of key-value pairs in input_dict. This is because we need to sort the list of tuples using sorted.

Auxiliary Space: O(n), as a new list and dictionary need to be created to store the sorted key-value pairs.

RELATED ARTICLES

Most Popular

Recent Comments