1

Question:

A function that prints a dictionary as in the example below

recursive_dict = {

   'a': 1,
   'b': {
       'c': 1,
       'd': 4
   },
   'e':
   {
       'f': 3,
       'g':
       {
           'h': 5,
           'i': 6
       }
   },
   'j': 10
}
to
'a' - 1
'b' - 'c' - 1
'b' - 'd' - 4
'e' - 'f' - 3
'e' - 'g' - 'h' - 5
'e' - 'g' - 'i' - 6
'j' - 10

My Code:

Any criticism about the code is welcomed as i am a python beginner, or any way to make this easier to read. The code doesn't work after a higher level recurssion value as it keeps the old key. I think i have to detect when a dictionary is ending so i remove from the old_key the last key i concatenated. Another harder? idea is to convert the dictionary to a tree and then proceed to read all the branches of the tree.

def print_dictionary_tree(tree_dict, default_separator='-', branch="", old_key=""):
    for key, value in tree_dict.items():
        if type(value) != dict:
            if old_key == "":
                branch += f"%{key}% {default_separator} {value} \n"
            else:
                branch += f"{old_key} {default_separator} /{key}/ {default_separator} {value} \n"
        else:
            if old_key != "":
                old_key += f" {default_separator} %{key}%"
            elif key != value.get(key):
                old_key += f"^{key}^"
            branch += print_dictionary_tree(value, default_separator, "", old_key)
    return branch

Returns

'a' - 1 
'b' - 'c' - 1 
'b' - 'd' - 4 
'b' - 'e' - 'f' - 3 
'b' - 'e' - 'g' - 'h' - 5 
'b' - 'e' - 'g' - 'i' - 6 
'b' - 'e' - 'j' - 10 

Useful links i found similar but I wasn't able to repair my function:

List of branches of a Python tree

Loop through all nested dictionary values?

2 Answers2

1

Python generators make quick work of problems like these -

def traverse(d, path = []):
  if isinstance(d, dict):
    for (k, v) in d.items():
      yield from traverse(v, [*path, k])
  else:
    yield [*path, d]

input = ...                          # <- your dict

for p in traverse(input):
  print("-".join(map(str, p)))

Output -

a-1
b-c-1
b-d-4
e-f-3
e-g-h-5
e-g-i-6
j-10
Mulan
  • 129,518
  • 31
  • 228
  • 259
0

You just need to clear the old_keys each time you quit a inner dictionary:


def print_dictionary_tree(tree_dict, default_separator='-', branch="", old_key=""):
    for key, value in tree_dict.items():
        if type(value) != dict:
            if old_key == "":
                branch += f"{key} {default_separator} {value} \n"
            else:
                branch += f"{old_key} {default_separator} {key} {default_separator} {value} \n"
        else:
            if old_key != "":
                old_key += f" {default_separator} {key}"
            elif key != value.get(key):
                old_key += f"{key}"
            branch += print_dictionary_tree(value, default_separator, "", old_key)
            old_key = old_key.replace(key, "") ## < --- HERE
    return branch

The result:

a - 1
b - c - 1
b - d - 4
e - f - 3
e - g - h - 5
e - g - i - 6
j - 10
samthegolden
  • 1,366
  • 1
  • 10
  • 26