Python – Convert String to Nested Dictionaries

Sometimes, while working with dictionaries, we can have a problem in which we need to convert a String to nested dictionary, each separator occurrence meaning a new nesting. This is a particular problem but can occur in data domains and day-day programming. Let’s discuss certain way in which this task can be done.

Method : Using loop + recursion
This is way in which this task can be performed. In this, we recur for nesting of dictionary as we encounter a separator occurrence.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Convert String to Nested Dictionaries
# Using loop
  
def helper_fnc(test_str, sep):
  if sep not in test_str:
    return test_str
  key, val = test_str.split(sep, 1)
  return {key: helper_fnc(val, sep)}
  
# initializing string
test_str = 'gfg_is_best_for_geeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing separator
sep = '_'
  
# Convert String to Nested Dictionaries
# Using loop
res = helper_fnc(test_str, sep)
  
# printing result 
print("The nested dictionary is : " + str(res)) 

chevron_right


Output :

The original string is : gfg_is_best_for_geeks
The nested dictionary is : {'gfg': {'is': {'best': {'for': 'geeks'}}}}

 

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

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.