Skip to content
Related Articles

Related Articles

Python Program to Re-assign a dictionary based on path relation
  • Last Updated : 11 Dec, 2020

Given a dictionary, the task is to formulate a python program to re-assign it using a path relation among its keys and values, i.e value of one key is key to another. 

Example:

Input : test_dict = {3 : 4, 5 : 6, 4 : 8, 6 : 9, 8 : 10} 
Output : {3: 10, 5: 9, 4: 10, 6: 9, 8: 10} 
Explanation : key 3 has value 4. key 4 has value 8. key 8 has value 10. there is no key 10 thus in the new dictionary key will have value 10.

Similarly, key 5 has value 6. key 6 has value 9. There is no key 9 hence in the new dictionary key 5 will have value 9.

Approach: Using loop and keys()



In this, we iterate for each key and find its depth, by using external function for repeated checking for each value being key of other item in dictionary.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

def find_depth(ele, dicti):
  
    # finding depth
    for idx in range(len(list(dicti.keys()))):
  
        # assigning value as key if found
        if ele in list(dicti.keys()):
            ele = dicti[ele]
    return ele
  
  
# initializing dictionary
test_dict = {3: 4, 5: 6, 4: 8, 6: 9, 8: 10}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
res = dict()
  
# iterating for each key
for key, val in list(test_dict.items()):
    test_dict.pop(key)
    res[key] = find_depth(val, test_dict)
  
# printing result
print("The reassigned dictionary : " + str(res))

chevron_right


Output:

The original dictionary is : {3: 4, 5: 6, 4: 8, 6: 9, 8: 10}

The reassigned dictionary : {3: 10, 5: 9, 4: 10, 6: 9, 8: 10}

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :