Python – Rearrange dictionary for consective 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 succeded by equal key.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Rearrange dictionary for consective value-keys
# Using loop + keys()
  
# initializing dictionary
test_dict = {1 : 3, 4 : 5, 3 : 4, 5 : 6}
  
# printing original dictionary
print("The original dictionary : " + str(test_dict))
  
# Rearrange dictionary for consective value-keys
# Using loop + keys()
temp = list(test_dict.keys())[0]
res = {}
while len(test_dict) > len(res):
    res[temp] = temp = test_dict[temp]
  
# printing result 
print("The rearranged dictionary : " + str(res)) 

chevron_right


Output :



The original dictionary : {1: 3, 4: 5, 3: 4, 5: 6}
The rearranged dictionary : {1: 3, 3: 4, 4: 5, 5: 6}

 

Method #2 : Using dictionary comprehension + accumulate()
The combination of above functions can be used to solve this problem. In this, we perform the task fo pairing using accumulate and rearrange new dictionary using dictionary comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Rearrange dictionary for consective value-keys
# Using dictionary comprehension + accumulate()
from itertools import accumulate
  
# initializing dictionary
test_dict = {1 : 3, 4 : 5, 3 : 4, 5 : 6}
  
# printing original dictionary
print("The original dictionary : " + str(test_dict))
  
# Rearrange dictionary for consective value-keys
# Using dictionary comprehension + accumulate()
res = {key : test_dict[key] for key in accumulate(test_dict, 
                              lambda key, x :test_dict[key])}
  
# printing result 
print("The rearranged dictionary : " + str(res)) 

chevron_right


Output :

The original dictionary : {1: 3, 4: 5, 3: 4, 5: 6}
The rearranged dictionary : {1: 3, 3: 4, 4: 5, 5: 6}



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.