
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Convert nested dictionary into flattened dictionary?
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' }
- Related Articles
- Python - Convert flattened dictionary into nested dictionary
- Python - Convert list of nested dictionary into Pandas Dataframe
- Python Pandas - Convert Nested Dictionary to Multiindex Dataframe
- Python - Convert a set into dictionary
- Convert Nested Tuple to Custom Key Dictionary in Python
- Convert dictionary object into string in Python
- Convert two lists into a dictionary in Python
- Convert string dictionary to dictionary in Python
- How to create nested Python dictionary?
- How to convert Javascript dictionary to Python dictionary?
- How I can convert a Python Tuple into Dictionary?
- How to sort a nested Python dictionary?
- Python program to convert a list of tuples into Dictionary
- How to recursively iterate a nested Python dictionary?
- How to count elements in a nested Python dictionary?
