
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Nested Dictionary into Flattened Dictionary in Python
As the world embraces more unstructured data, we come across many formats of data where the data structure can be deeply nested like nested JSONS. Python has the ability to deal with nested data structure by concatenating the inner keys with outer keys to flatten the data. In this article we will take a nested dictionary and flattened it.
Using a recursive approach
In this approach we design a function to recursively process each item in the dictionary. We pass the dictionary, design a place holder for the output dictionary, the key and separator as parameters. We use the isinstance to check if the next item itself is a dictionary and then pass it through the recursive call if it is also a dictionary.
Example
dictA = { "id": "0001", "name": "hotdog", "image": { "url": "images/0001.jpg", "thumbnail": { "url": "images/thumbnails/0001.jpg", "height,width": "2x4" } } } def dict_flatten(in_dict, dict_out=None, parent_key=None, separator="_"): if dict_out is None: dict_out = {} for k, v in in_dict.items(): k = f"{parent_key}{separator}{k}" if parent_key else k if isinstance(v, dict): dict_flatten(in_dict=v, dict_out=dict_out, parent_key=k) continue dict_out[k] = v return dict_out final_dict = dict_flatten(dictA) print(final_dict)
Output
Running the above code gives us the following result −
{ 'id': '0001', 'name': 'hotdog', 'image_url': 'images/0001.jpg', 'image_thumbnail_url': 'images/thumbnails/0001.jpg', 'image_thumbnail_height,width': '2x4' }
With cherrypicker
This is a module which can be directly used by giving it the dictionary as input. The default separator is -.
Example
from cherrypicker import CherryPicker dictA = { "id": "0001", "name": "hotdog", "image": { "url": "images/0001.jpg", "thumbnail": { "url": "images/thumbnails/0001.jpg", "height,width": "2x4" } } } picker = CherryPicker(dictA) print(picker.flatten().get())
Output
Running the above code gives us the following result −
{ 'id': '0001', 'name': 'hotdog', 'image_url': 'images/0001.jpg', 'image_thumbnail_url': 'images/thumbnails/0001.jpg', 'image_thumbnail_height, width': '2x4' }