One possible approach to flatten a nested dictionary is to compress its keys. A nested dictionary can contain several other nested dictionaries inside it.
Although a nested dictionary helps us store hierarchical data efficiently, we cannot interpret and understand the nested dictionary in its original form.
We need to either print it in an understandable format or flatten it. While flattening the nested dictionary, we can compress the keys of the dictionary separated by an underscore or any other separator you like. Doing this will result in a dictionary that is human-readable and understandable.
In this tutorial, we will learn all the possible ways to flatten a nested dictionary.
Before that, visit this post to learn more about dictionaries.
What Is a Nested Dictionary?
A nested dictionary is also a dictionary that can contain several other dictionaries inside it. A nested dictionary can be created using a for loop or even manually.
Read this article to learn how to create a nested dictionary using for loop.
Let us see a simple example of a nested dictionary.
nesteddict={'a': {
'b': 1,
'c': {
'd': 2,
'e': 3
}
},
'f': 4
}
print("The nested dictionary is:")
print(nesteddict)
ks=nesteddict.keys()
print("The keys of the nested dictionary are:\n",ks)
vs=nesteddict.values()
print("The values of the nested dictionary are:\n",vs)
In the above code, we have created a variable called nesteddict
to store the nested dictionary. The nested dictionary contains five keys- a,b,c,d,e and some corresponding values associated with them. But if you notice, the keys b,c,d, and e are enclosed within the curly braces of another key so that they will be treated as the values of that key. To better understand this concept, we are printing the keys and values of the dictionary separately using keys()
and values()
functions.

We can observe that the outer keys- a and f are returned as the keys while b,c,d, and e, enclosed within the curly braces, are treated as values.
Since this example is a simple nested dictionary, we would not have any issue understanding it. But that will not be the case with dictionaries containing complex dictionaries inside them. We have an approach to print the nested dictionaries in a pretty manner.
Read this post to learn how to print nested dictionaries.
How to Flatten a Nested Dictionary?
We will use the example we have seen before and flatten it by compressing its keys.
Flatten a Nested Dictionary With a User-Defined Function
We will create a user-defined function that compresses the keys and flattens the nested dictionary.
nesteddict = {
'a': {
'b': 1,
'c': {
'd': 2,
'e': 3}},
'f': 4}
def flattendict(d, parentkey='', sep='_'):
items = []
for k, v in d.items():
newkey = parentkey + sep + k if parentkey else k
if isinstance(v, dict):
items.extend(flatten_dict(v, newkey, sep=sep).items())
else:
items.append((newkey, v))
return dict(items)
flattened_dict = flattendict(nesteddict)
print("The flattened dictionary is:\n",flattened_dict)
In the first line, we create a nested dictionary stored in a variable called nesteddict.
Next, we create a function using the def keyword. That takes an iterator variable d, and the parentkey keeps a tab on the nested dictionary while going through each level. The separator we use here is an underscore, which separates the nested keys from the underscore.
An empty list called items is created to append the flattened dictionary. Next, we initialize a for loop to run through every key in the dictionary and append the compressed keys to the empty list.
Then, the nested dictionary is passed as an argument to the user-defined function. The flattened dictionary is printed in the last line.

Flatten a Nested Dictionary Using Flatten-json
The flatten-json is a third-party library that transforms your complex data into a table. To use the flatten-JSON library, we must first install and import the package using the following code.
!pip install flatten_json
import flatten_json
After we have imported the package, we can directly use it to flatten our nested dictionary.
nesteddict = {
'a': {
'b': 1,
'c': {
'd': 2,
'e': 3
}
},
'f': 4
}
flattened_dict = flatten_json.flatten(nesteddict, separator='_')
print("The flatttened dictionary is:\n",flattened_dict)
We are using the same nested dictionary and creating a variable flattened_dict
to store the new dictionary obtained by passing this dictionary to the flatten_json
with an underscore separator.
This flattened dictionary is printed in the following line.

Flatten a Nested Dictionary Using Pandas
We can use the JSON normalize function of the Pandas library to flatten the nested dictionary. Like the two examples above, the keys are compressed with an underscore.
import pandas as pd
nesteddict = {
'a': {
'b': 1,
'c': {
'd': 2,
'e': 3
}
},
'f': 4
}
df = pd.json_normalize(nesteddict, sep='_')
flattened_dict = df.to_dict(orient='records')[0]
print("The flattened dictionary is :\n",flattened_dict)
In the first line, we are importing the Pandas library as pd, the standard alias name for the library.
Next, we are initializing a variable called nesteddict to store the nested dictionary.
This dictionary is normalized by the pd.json_normalize
method and is stored in a variable called df. df is then returned as a dictionary and is printed in the last line.

Flatten a Nested Dictionary Using Prettyprint
The pretty print module can make the flattened dictionary look prettier.
import pprint
nested_dict = {
'a': {
'b': 1,
'c': {
'd': 2,
'e': 3
}
},
'f': 4
}
flattened_dict = flattendict(nested_dict)
print("The pretty flattened dictionary is:")
pprint.pprint(flattened_dict)
This example is an extension of method 1- User-defined function. After the nested dictionary is passed to the function to return a flattened dictionary, this flattened dictionary is passed to the pprint module to make it prettier.

Conclusion
To conclude, we have learned about a nested dictionary and how a nested dictionary helps us to store hierarchical data at different levels. But it can be difficult for us humans to understand the data possibly.
We need to either print it in an understandable format or flatten it. While flattening the nested dictionary, we can compress the keys of the dictionary separated by an underscore or any other separator you like. Doing this will result in a dictionary that is human-readable and understandable.
We have seen an example of a simple nested dictionary and understood how a nested dictionary stores the data.
Next, we have seen a few approaches to flattening a nested dictionary by compressing the keys.
In the first approach, we have created a user-defined function that separates the nested keys using a separator and flattens the nested dictionary.
Next, we used a third-party library called flatten-json to flatten the nested dictionary. The new dictionary has the original dictionary but has the nested keys formatted with a separator.
In the third approach, we have used the Pandas Library’s normalize method to flatten the dictionary.
Lastly, the pretty print module made the flattened dictionary even prettier.
References
More info about flatten-json can be found here.
Visit the official Python documentation to learn more about PrettyPrint.
Refer to this stack overflow answer chain on the same topic.