Is there a more efficient and readable alternative to nested dictionaries with different keys in python?

Viewed 154

I have a data structure in my application which takes the form of an enormous nested dictionary. It is difficult to work with and visualize, and I'm trying to see if there is an alternative. The structure is as follows:

{ top_level_key: {key: value,
                  key: value,
                  ....
                  other_dict_key: {other_key: other_value,
                                   other_key: other_value,
                                   other_key: other_value}
  top_level_key: {...}
}

I thought about using a pandas multi index, but the fact that the keys are different in the inner dictionary makes this impossible (I think), or just not useful, because I would end up with a bunch of null values for the outer dictionary entries for those keys.

Any ideas would be much appreciated.

1 Answers

If you just need to visualize your data more easily, I would suggest using the pprint module from Python standard library, which acts as a pretty printer especially useful for nested data structures.

See this part of the documentation for usage example.

Related