Flatten and Remove Keys from Dictionary
Last Updated :
04 Feb, 2025
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.
Similar Reads
Python - Remove double quotes from dictionary keys Given dictionary with string keys, remove double quotes from it. Input : test_dict = {'"Geeks"' : 3, '"g"eeks' : 9} Output : {'Geeks': 3, 'geeks': 9} Explanation : Double quotes removed from keys. Input : test_dict = {'"Geeks"' : 3} Output : {'Geeks': 3} Explanation : Double quotes removed from keys
6 min read
Python Remove Dictionary Item Sometimes, we may need to remove a specific item from a dictionary to update its structure. For example, consider the dictionary d = {'x': 100, 'y': 200, 'z': 300}. If we want to remove the item associated with the key 'y', several methods can help achieve this. Letâs explore these methods.Using pop
2 min read
Python - Remove Item from Dictionary There are situations where we might want to remove a specific key-value pair from a dictionary. For example, consider the dictionary d = {'x': 10, 'y': 20, 'z': 30}. If we need to remove the key 'y', there are multiple ways to achieve this. Let's discuss several methods to remove an item from a dict
3 min read
Remove Kth Key from Dictionary - Python We are given a dictionary we need to remove Kth key from the dictionary. For example, we are given a dictionary d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'} we need to remove the key2 so that the output should be {'key1': 'value1', 'key3': 'value3', 'key4': 'value4'}.
3 min read
Python Remove Item from Dictionary by Key Dictionaries in Python store data as key-value pairs. Often, we need to remove a specific key-value pair to modify or clean the dictionary. For instance, consider the dictionary d = {'a': 1, 'b': 2, 'c': 3}; we might want to remove the key 'b'. Let's explore different methods to achieve this.Using d
3 min read