Python – Rearrange dictionary for consecutive value-keys
Sometimes, while working with Python dictionaries, we can have a problem in which we need to rearrange dictionary keys so as a value is followed by same key in dictionary. This problem can have application in competitive programming algorithms and Graph problems. Let’s discuss certain ways in which this task can be performed.
Input : test_dict = {1 : 2, 3 : 2, 2 : 3}
Output : {1: 2, 2: 3, 3: 2}
Input : test_dict = {1 : 2}
Output : {1 : 2}
Method #1 : Using loop + keys()
The combination of above functions can be used to solve this problem. In this, we extract the dictionary keys using keys() and iterate till we find value succeeded by equal key.
Python3
test_dict = { 1 : 3 , 4 : 5 , 3 : 4 , 5 : 6 }
print ( "The original dictionary : " + str (test_dict))
temp = list (test_dict.keys())[ 0 ]
res = {}
while len (test_dict) > len (res):
res[temp] = temp = test_dict[temp]
print ( "The rearranged dictionary : " + str (res))
|
Output :
The original dictionary : {1: 3, 4: 5, 3: 4, 5: 6}
The rearranged dictionary : {1: 3, 3: 4, 4: 5, 5: 6}
Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2 : Using dictionary comprehension + accumulate()
The combination of above functions can be used to solve this problem. In this, we perform the task of pairing using accumulate and rearrange new dictionary using dictionary comprehension.
Python3
from itertools import accumulate
test_dict = { 1 : 3 , 4 : 5 , 3 : 4 , 5 : 6 }
print ( "The original dictionary : " + str (test_dict))
res = {key : test_dict[key] for key in accumulate(test_dict,
lambda key, x :test_dict[key])}
print ( "The rearranged dictionary : " + str (res))
|
Output :
The original dictionary : {1: 3, 4: 5, 3: 4, 5: 6}
The rearranged dictionary : {1: 3, 3: 4, 4: 5, 5: 6}
Method #3: Using sorted() and list comprehension
Step-by-step algorithm:
- Initialize the dictionary and the keys.
- Sort the keys of the dictionary.
- Retrieve the values for each key in the sorted keys list and store them in a separate list.
- Create a new dictionary by zipping the sorted keys and values list.
- Print the rearranged dictionary.
Python3
test_dict = { 1 : 3 , 4 : 5 , 3 : 4 , 5 : 6 }
print ( "The original dictionary : " + str (test_dict))
keys = sorted (test_dict.keys())
values = [test_dict[k] for k in keys]
res = dict ( zip (keys, values))
print ( "The rearranged dictionary : " + str (res))
|
Output
The original dictionary : {1: 3, 4: 5, 3: 4, 5: 6}
The rearranged dictionary : {1: 3, 3: 4, 4: 5, 5: 6}
Time complexity: O(n log n) where n represents the number of key-value pairs in the input dictionary test_dict.
Space complexity: O(n) where n represents the number of key-value pairs in the input dictionary test_dict.
Method #4: Using sorted and OrderedDict
- Importing the OrderedDict class from the collections module.
- Creating a dictionary test_dict with key-value pairs.
- Printing the original dictionary using print() and str().
- Creating an ordered dictionary res with sorted keys and corresponding values, using sorted() method to sort the items in test_dict.
- Converting the ordered dictionary to a regular dictionary using dict() method.
- Printing the rearranged dictionary using print() and str().
Python3
from collections import OrderedDict
test_dict = { 1 : 3 , 4 : 5 , 3 : 4 , 5 : 6 }
print ( "The original dictionary : " + str (test_dict))
res = OrderedDict( sorted (test_dict.items()))
res = dict (res)
print ( "The rearranged dictionary : " + str (res))
|
Output
The original dictionary : {1: 3, 4: 5, 3: 4, 5: 6}
The rearranged dictionary : {1: 3, 3: 4, 4: 5, 5: 6}
Time complexity: O(n log n) where n represents the number of key-value pairs in the input dictionary test_dict.
Auxiliary Space: O(n) where n represents the number of key-value pairs in the input dictionary test_dict.
Last Updated :
27 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...