Python - Convert Nested Dictionary into Flattened Dictionary Last Updated : 30 Jan, 2025 Comments Improve Suggest changes Like Article Like Report We are given a nested dictionary we need to flatten the dictionary into single dictionary. For example, we are given a nested dictionary a = {'a': 1, 'b': {'x': 2, 'y': {'z': 3}}, 'c': {'m': 4} } we need to flatten the dictionary so that output becomes {'a': 1, 'c_m': 4, 'b_x': 2, 'b_y_z': 3}. We can use stack and queues for flattening it.Using a StackUsing a stack, we push tuples of the current dictionary and its parent key, then pop them to flatten nested dictionaries by concatenating keys. If a value is a dictionary then it's pushed back onto the stack for further processing. Python a = { 'a': 1, 'b': {'x': 2, 'y': {'z': 3}}, 'c': {'m': 4} } f = {} stack = [(a, '')] # Stack holds tuples of (current_dict, current_key) while stack: c, p = stack.pop() for k, v in c.items(): new_key = f"{p}_{k}" if p else k if isinstance(v, dict): stack.append((v, new_key)) # Push the nested dictionary onto the stack else: f[new_key] = v # Add to the flattened dictionary print(f) Output{'a': 1, 'c_m': 4, 'b_x': 2, 'b_y_z': 3} Explanation:Stack stores tuples of the current dictionary and its parent key; we process each dictionary, concatenating keys and adding key-value pairs to the flattened dictionary.If a value is a nested dictionary then it is pushed back onto the stack for further flattening, otherwise the key-value pair is added to the result.Using a QueueUsing a queue, we enqueue tuples of the current dictionary and its parent key, then dequeue them to process the dictionary and flatten it. If a value is a nested dictionary, it's enqueued for further processing. Python from collections import deque a = { 'a': 1, 'b': {'x': 2, 'y': {'z': 3}}, 'c': {'m': 4} } f = {} queue = deque([(a, '')]) # Queue holds tuples of (current_dict, current_key) while queue: c, p = queue.popleft() for k, v in c.items(): new_key = f"{p}_{k}" if p else k if isinstance(v, dict): queue.append((v, new_key)) # Add nested dictionaries to the queue else: f[new_key] = v print(f) Output{'a': 1, 'b_x': 2, 'c_m': 4, 'b_y_z': 3} Explanation:Queue stores tuples of the current dictionary and its parent key; we dequeue them to process and flatten the dictionary by concatenating keys.If a value is a nested dictionary then it is enqueued for further processing otherwise the key-value pair is added to the flattened result. Comment More infoAdvertise with us Next Article Python - Convert Nested Dictionary into Flattened Dictionary garg_ak0109 Follow Improve Article Tags : Python Python Programs Python dictionary-programs Python-nested-dictionary Practice Tags : python Similar Reads Python | Convert flattened dictionary into nested dictionary Given a flattened dictionary, the task is to convert that dictionary into a nested dictionary where keys are needed to be split at '_' considering where nested dictionary will be started. Method #1: Using Naive Approach Step-by-step approach : Define a function named insert that takes two parameters 8 min read Python - Convert Flat dictionaries to Nested dictionary Sometimes, while working with records, we can have a problem in which we need to perform the task of conversion of multiple flat dictionaries to a single nested dictionary. This can have applications in many domains in which data is used extensively. Let's discuss certain ways by which we can conver 4 min read Convert Nested Dictionary to List in Python In this article, weâll explore several methods to Convert Nested Dictionaries to a List in Python. List comprehension is the fastest and most concise way to convert a nested dictionary into a list.Pythona = { "a": {"x": 1, "y": 2}, "b": {"x": 3, "y": 4}, } # Convert nested dictionary to a list of li 3 min read Convert Dictionary Value list to Dictionary List Python Sometimes, while working with Python Dictionaries, we can have a problem in which we need to convert dictionary list to nested records dictionary taking each index of dictionary list value and flattening it. This kind of problem can have application in many domains. Let's discuss certain ways in whi 9 min read Python - Convert Dictionaries List to Order Key Nested dictionaries Given list of dictionaries, our task is to convert a list of dictionaries into a dictionary where each key corresponds to the index of the dictionary in the list and the value is the dictionary itself. For example: consider this list of dictionaries: li = [{"Gfg": 3, 4: 9}, {"is": 8, "Good": 2}] the 3 min read Convert List of Dictionaries to Dictionary of Lists - Python We are given a list of dictionaries we need to convert it to dictionaries of lists. For example, we are given a list of dictionaries li = [{'manoj': 'java', 'bobby': 'python'}, {'manoj': 'php', 'bobby': 'java'}, {'manoj': 'cloud', 'bobby': 'big-data'}] we need to convert this to dictionary of list s 3 min read Python - Convert list of dictionaries to Dictionary Value list We are given a list of dictionaries we need to convert it to dictionary. For example, given a list of dictionaries: d = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'b': 6}], the output should be: {'a': [1, 3, 5], 'b': [2, 4, 6]}.Using Dictionary ComprehensionUsing dictionary comprehension, we can 3 min read Remove Duplicate Dictionaries from Nested Dictionary - Python We are given a nested dictionary we need to remove the duplicate dictionaries from the nested dictionary. For example we are given a nested dictionary d = {'key1': [{'a': 1}, {'b': 2}, {'a': 1}], 'key2': [{'x': 3}, {'y': 4}]} we need to remove the duplicate dictionary from this dictionary so output 4 min read Convert Nested Tuple to Custom Key Dictionary - Python The task is to convert a nested tuple into a dictionary with custom keys. Each tuple in the nested structure contains multiple elements, and the goal is to map these elements to specific keys in a dictionary.For example, given the tuple a = ((4, 'Gfg', 10), (3, 'is', 8), (6, 'Best', 10)), the goal i 3 min read Python - Convert Nested dictionary to Mapped Tuple The task is to convert a nested dictionary into a mapping where each key's values are transformed into tuples. This involves taking the inner dictionaries and creating tuples for each key-value pair across the nested structure. If a key appears in multiple inner dictionaries, its values should be gr 4 min read Like