Open In App

Flatten and Remove Keys from Dictionary

Last Updated : 04 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a dictionary we need to flatten and remove keys from it. For example, we are given a dictionary d = {'a': {'b': 1, 'c': {'d': 2}}, 'e': 3} we need to flatten and remove the keys so that the output should be {'e': 3, 'a.b': 1, 'a.c.d': 2}. We can use itertools and various other approaches.

Using Loops for Flattening

Nested dictionaries are processed iteratively using a stack. Each key is combined with its parent key to form a flattened key. Non-dictionary values are directly added to result while nested dictionaries are pushed back onto stack for further processing.

Python
d = {"a": 1, "b": {"c": 2, "d": {"e": 3, "f": 1}, "g": 1}, "h": [1, {"i": 1, "j": 4}]}
a = 1  
s = [d]  
while s:
    c = s.pop()  
    k = [key for key, val in c.items() if val == a]  

    # Delete the keys that have value 'a'
    for key in k:  
        del c[key]  

    # Check for nested dictionaries or lists in  current dictionary
    for key, val in c.items():
        if isinstance(val, dict):
            s.append(val)  
        
        # If the value is a list, check its items for dictionaries and add them to the stack
        elif isinstance(val, list):
            for item in val:
                if isinstance(item, dict):  
                    # Add dictionaries found in the list to the stack
                    s.append(item)  
print(d)  

Output
{'e': 3, 'a.b': 1, 'a.c.d': 2}

Explanation:

  • Stack s is used to iteratively process nested dictionaries constructing hierarchical keys (new_key) by appending the current key to parent_key with a dot separator.
  • Nested dictionaries are added back to the stack while non-dictionary values are stored in flattened dictionary f with their constructed keys.

Using a Queue

We can use a queue for breadth-first traversal instead of depth-first traversal.

Python
from collections import deque

d = {'a': {'b': 1, 'c': {'d': 2}}, 'e': 3}
q = deque([(d, '')])
f = {}

while q:
    c, p = q.popleft() 
    for key, value in c.items():
        new_key = f"{p}.{key}" if p else key
        
        if isinstance(value, dict):
             # Add nested dictionary to the queue
            q.append((value, new_key)) 
            
        else:
            # Add flattened key-value pair
            f[new_key] = value  

print(f)

Output
{'e': 3, 'a.b': 1, 'a.c.d': 2}

Explanation:

  • Queue (q) processes dictionaries level by level constructing new keys by combining the parent key (p) with current key.
  • Nested dictionaries are added back to the queue while non-dictionary values are added to flattened dictionary (f) with their full keys.

Flattening Using Itertools

Itertools can be used for flattening if keys and values are extracted iteratively.

Python
from itertools import chain
d = {'a': {'b': 1, 'c': {'d': 2}}, 'e': 3}
f = {}
s = [(d, '')]
while s:
    c, p = s.pop()
    
    # Iterate over all key-value pairs in the current dictionary
    for key, value in c.items():
        # Construct the new key by combining parent key and current key
        new_key = f"{p}.{key}" if p else key
        
        #add it back to the stack with the new key
        if isinstance(value, dict):
             s.append((value, new_key))
            
        else:
          
            # If the value is not a dictionary
            f[new_key] = value
print(f)

Output
{'e': 3, 'a.b': 1, 'a.c.d': 2}

Explanation:

  • Stack processes the dictionary in depth-first order constructing new keys by combining the parent key with current key.
  • Nested dictionaries are pushed back to the stack, and non-dictionary values are added to the flattened result.

Next Article
Practice Tags :

Similar Reads