Python – Flatten Nested Keys

Sometimes, while working with Python data, we can have a problem in which we need to perform the flattening of certain keys in nested list records. This kind of problem occurs while data preprocessing. Lets discuss certain ways in which this task can be performed.

Method #1 : Using loop
This is brute force method to perform this task. In this, we construct new dictionary by assigning base keys and then perform the flattening of inner key elements using nested loop.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Flatten Nested Keys
# Using loop
  
# initializing list
test_list = [{'Gfg' 1, 'id' : 1, 'data' : [{'rating' : 7, 'price' : 4},
             {'rating' : 17, 'price' : 8}]}, 
             {'Gfg' 1, 'id' : 2, 'data' : [{'rating' : 18, 'price' : 19}]}]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Flatten Nested Keys
# Using loop
res = []
for sub in test_list:
    temp1 = {
      'Gfg': sub['Gfg'],
      'id': sub['id']
    }
    for data in sub.get('data', []):
        res.append({
            **temp1,
            'rating': data['rating'],
            'price': data['price']})
  
# printing result 
print("The flattened list : " + str(res)) 

chevron_right


Output :

The original list is : [{‘data’: [{‘rating’: 7, ‘price’: 4}, {‘rating’: 17, ‘price’: 8}], ‘id’: 1, ‘Gfg’: 1}, {‘data’: [{‘rating’: 18, ‘price’: 19}], ‘id’: 2, ‘Gfg’: 1}]
The flattened list : [{‘price’: 4, ‘rating’: 7, ‘id’: 1, ‘Gfg’: 1}, {‘price’: 8, ‘rating’: 17, ‘id’: 1, ‘Gfg’: 1}, {‘price’: 19, ‘rating’: 18, ‘id’: 2, ‘Gfg’: 1}]

 



Method #2 : Using list comprehension + zip() + itemgetter()
The combination of above functions can be used to perform this task. In this, we extract the required pairs using itemgetter() and combine pairs using zip(). The compilation of data is using list comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Flatten Nested Keys
# Using list comprehension + zip() + itemgetter()
from operator import itemgetter
  
# initializing list
test_list = [{'Gfg' 1, 'id' : 1, 'data' : [{'rating' : 7, 'price' : 4},
             {'rating' : 17, 'price' : 8}]}, 
             {'Gfg' 1, 'id' : 2, 'data' : [{'rating' : 18, 'price' : 19}]}]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing base and flatten keys 
base_keys = 'Gfg', 'id'
flatten_keys = 'rating', 'price'
  
# Flatten Nested Keys
# Using list comprehension + zip() + itemgetter()
res = [dict(zip(base_keys + flatten_keys, itemgetter(*base_keys)(sub) + itemgetter(*flatten_keys)(data))) for sub in test_list for data in sub['data']]
  
# printing result 
print("The flattened list : " + str(res)) 

chevron_right


Output :

The original list is : [{‘data’: [{‘rating’: 7, ‘price’: 4}, {‘rating’: 17, ‘price’: 8}], ‘id’: 1, ‘Gfg’: 1}, {‘data’: [{‘rating’: 18, ‘price’: 19}], ‘id’: 2, ‘Gfg’: 1}]
The flattened list : [{‘price’: 4, ‘rating’: 7, ‘id’: 1, ‘Gfg’: 1}, {‘price’: 8, ‘rating’: 17, ‘id’: 1, ‘Gfg’: 1}, {‘price’: 19, ‘rating’: 18, ‘id’: 2, ‘Gfg’: 1}]




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.