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 G 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 Like