The Wayback Machine - https://web.archive.org/web/20240828111124/https://www.geeksforgeeks.org/python-convert-nested-dictionary-into-flattened-dictionary/
Open In App

Python | Convert nested dictionary into flattened dictionary

Last Updated : 04 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a nested dictionary, the task is to convert this dictionary into a flattened dictionary where the key is separated by ‘_’ in case of the nested key to be started.

Method #1: Using Naive Approach 

Step-by-step approach :

  1. The function checks if the input dd is a dictionary. If it is, then it iterates over each key-value pair in the dictionary, and calls the flatten_dict function recursively on the value of each key. It concatenates the original key and the returned key from the recursive call with the separator, and uses that as the new key in the flattened dictionary. It sets the value of the new key to the returned value from the recursive call.
  2. If the input dd is not a dictionary, the function creates a new dictionary with a single key-value pair, where the key is the prefix argument (which is the path to the current value in the original nested dictionary), and the value is the input dd itself.
  3. The code then initializes a nested dictionary ini_dict with some sample data.
  4. The code prints the initial dictionary ini_dict.
  5. The code calls the flatten_dict function on the ini_dict dictionary and prints the resulting flattened dictionary.

Python3




# Python code to demonstrate
# conversion of nested dictionary
# into flattened dictionary
 
# code to convert ini_dict to flattened dictionary
# default separator '_'
def flatten_dict(dd, separator ='_', prefix =''):
    return { prefix + separator + k if prefix else k : v
             for kk, vv in dd.items()
             for k, v in flatten_dict(vv, separator, kk).items()
             } if isinstance(dd, dict) else { prefix : dd }
         
# initialising_dictionary
ini_dict = {'geeks': {'Geeks': {'for': 7}},
            'for': {'geeks': {'Geeks': 3}},
            'Geeks': {'for': {'for': 1, 'geeks': 4}}}
 
# printing initial dictionary
print ("initial_dictionary", str(ini_dict))
 
 
# printing final dictionary
print ("final_dictionary", str(flatten_dict(ini_dict)))


Output: 
initial_dictionary {‘geeks’: {‘Geeks’: {‘for’: 7}}, ‘Geeks’: {‘for’: {‘geeks’: 4, ‘for’: 1}}, ‘for’: {‘geeks’: {‘Geeks’: 3}}} 
final_dictionary {‘Geeks_for_for’: 1, ‘geeks_Geeks_for’: 7, ‘for_geeks_Geeks’: 3, ‘Geeks_for_geeks’: 4} 
 

Time complexity: O(n^2), where n is the total number of keys in the nested dictionary.
Auxiliary space: O(n), where n is the total number of keys in the nested dictionary. The auxiliary space is used to store the result of the flatten dictionary in the form of a new dictionary.

Method #2: Using mutableMapping 

Python3




# Python code to demonstrate
# conversion of nested dictionary
# into flattened dictionary
 
from collections import MutableMapping
 
# code to convert ini_dict to flattened dictionary
# default separator '_'
def convert_flatten(d, parent_key ='', sep ='_'):
    items = []
    for k, v in d.items():
        new_key = parent_key + sep + k if parent_key else k
 
        if isinstance(v, MutableMapping):
            items.extend(convert_flatten(v, new_key, sep = sep).items())
        else:
            items.append((new_key, v))
    return dict(items)
         
# initialising_dictionary
ini_dict = {'geeks': {'Geeks': {'for': 7}},
            'for': {'geeks': {'Geeks': 3}},
            'Geeks': {'for': {'for': 1, 'geeks': 4}}}
 
# printing initial dictionary
print ("initial_dictionary", str(ini_dict))
 
 
# printing final dictionary
print ("final_dictionary", str(convert_flatten(ini_dict)))


Output: 
initial_dictionary {‘Geeks’: {‘for’: {‘for’: 1, ‘geeks’: 4}}, ‘for’: {‘geeks’: {‘Geeks’: 3}}, ‘geeks’: {‘Geeks’: {‘for’: 7}}} 
final_dictionary {‘Geeks_for_geeks’: 4, ‘for_geeks_Geeks’: 3, ‘geeks_Geeks_for’: 7, ‘Geeks_for_for’: 1} 
 

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3: Using Python Generators  

Python3




# Python code to demonstrate
# conversion of nested dictionary
# into flattened dictionary
 
my_map = {"a" : 1,
        "b" : {
            "c": 2,
            "d": 3,
            "e": {
                "f":4,
                6:"a",
                5:{"g" : 6},
                "l":[1,"two"]
            }
        }}
 
# Expected Output
# {'a': 1, 'b_c': 2, 'b_d': 3, 'b_e_f': 4, 'b_e_6': 'a', 'b_e_5_g': 6, 'b_e_l': [1, 'two']}
 
 
ini_dict = {'geeks': {'Geeks': {'for': 7}},
            'for': {'geeks': {'Geeks': 3}},
            'Geeks': {'for': {'for': 1, 'geeks': 4}}}
 
# Expected Output
# {‘Geeks_for_geeks’: 4, ‘for_geeks_Geeks’: 3, ‘Geeks_for_for’: 1, ‘geeks_Geeks_for’: 7}
 
def flatten_dict(pyobj, keystring=''):
    if type(pyobj) == dict:
        keystring = keystring + '_' if keystring else keystring
        for k in pyobj:
            yield from flatten_dict(pyobj[k], keystring + str(k))
    else:
        yield keystring, pyobj
 
print("Input : %s\nOutput : %s\n\n" %
     (my_map, { k:v for k,v in flatten_dict(my_map) }))
 
print("Input : %s\nOutput : %s\n\n" %
     (ini_dict, { k:v for k,v in flatten_dict(ini_dict) }))


Output: 
initial_dictionary {‘for’: {‘geeks’: {‘Geeks’: 3}}, ‘geeks’: {‘Geeks’: {‘for’: 7}}, ‘Geeks’: {‘for’: {‘for’: 1, ‘geeks’: 4}}} 
final_dictionary {‘Geeks_for_geeks’: 4, ‘for_geeks_Geeks’: 3, ‘Geeks_for_for’: 1, ‘geeks_Geeks_for’: 7} 
 

Time complexity: O(n), where n is the total number of keys in the nested dictionary
Auxiliary space: O(n), where n is the total number of keys in the nested dictionary. 

Method #4: Using recursion and a separator

  1. Define a function “flatten_dict” that takes in three parameters: dd (a dictionary), separator (a string, default value “_”), and prefix (a string, default value “”).
  2. Initialize an empty dictionary “res”.
  3. Iterate through each key-value pair in the dictionary “dd”.
  4. Check if the value associated with the current key is itself a dictionary or not. If it is a dictionary, then recursively call the “flatten_dict” function with the value as the new dictionary to be flattened, the separator and prefix updated with the current key and separator value.
  5. If the value is not a dictionary, then add the key-value pair to the “res” dictionary, where the key is the concatenation of prefix, separator and current key.
  6. Return the final “res” dictionary.

Python3




# code to convert ini_dict to flattened dictionary
def flatten_dict(dd, separator='_', prefix=''):
    res = {}
    for key, value in dd.items():
        if isinstance(value, dict):
            res.update(flatten_dict(value, separator, prefix + key + separator))
        else:
            res[prefix + key] = value
    return res
 
# initialising_dictionary
ini_dict = {'geeks': {'Geeks': {'for': 7}},
            'for': {'geeks': {'Geeks': 3}},
            'Geeks': {'for': {'for': 1, 'geeks': 4}}}
 
# printing initial dictionary
print("initial dictionary", str(ini_dict))
 
# flattening the dictionary
res = flatten_dict(ini_dict)
 
# printing final dictionary
print("final dictionary", str(res))
#This code is contributed by Vinay Pinjala.


Output

initial dictionary {'geeks': {'Geeks': {'for': 7}}, 'for': {'geeks': {'Geeks': 3}}, 'Geeks': {'for': {'for': 1, 'geeks': 4}}}
final dictionary {'geeks_Geeks_for': 7, 'for_geeks_Geeks': 3, 'Geeks_for_for': 1, 'Geeks_for_geeks': 4}

The time complexity of this algorithm is O(N), where N is the total number of keys in the nested dictionary. This is because the algorithm iterates through each key exactly once.
The space complexity is also O(N), where N is the total number of keys, since the algorithm creates a new dictionary “res” to store the flattened dictionary.

Method 5: Using a stack data structure

The idea is to iterate through the dictionary and push all nested dictionaries into a stack. Then, we can pop each dictionary from the stack, iterate through its key-value pairs, and append the key-value pairs to a flattened dictionary.

  1. Initialize an empty stack.
  2. Push the initial dictionary onto the stack.
  3. Initialize an empty flattened dictionary.
  4. While the stack is not empty, do the following:
    a. Pop the top dictionary from the stack.
    b. For each key-value pair in the popped dictionary, do the following:
    i. If the value is a dictionary, push it onto the stack with its key as the prefix.
    ii. Otherwise, add the key-value pair to the flattened dictionary with the prefix and separator (default is ‘_’).
  5. Return the flattened dictionary.

Python3




# code to convert ini_dict to flattened dictionary using stack data structure
def flatten_dict(dd, separator='_', prefix=''):
    stack = [(dd, prefix)]
    flat_dict = {}
     
    while stack:
        cur_dict, cur_prefix = stack.pop()
        for key, val in cur_dict.items():
            new_key = cur_prefix + separator + key if cur_prefix else key
            if isinstance(val, dict):
                stack.append((val, new_key))
            else:
                flat_dict[new_key] = val
                 
    return flat_dict
 
# initialising_dictionary
ini_dict = {'geeks': {'Geeks': {'for': 7}},
            'for': {'geeks': {'Geeks': 3}},
            'Geeks': {'for': {'for': 1, 'geeks': 4}}}
 
# printing initial dictionary
print("initial_dictionary", str(ini_dict))
 
# printing final dictionary
print("final_dictionary", str(flatten_dict(ini_dict)))


Output

initial_dictionary {'geeks': {'Geeks': {'for': 7}}, 'for': {'geeks': {'Geeks': 3}}, 'Geeks': {'for': {'for': 1, 'geeks': 4}}}
final_dictionary {'Geeks_for_for': 1, 'Geeks_for_geeks': 4, 'for_geeks_Geeks': 3, 'geeks_Geeks_for': 7}

Time complexity: O(n), where n is the total number of key-value pairs in the input nested dictionary.
Auxiliary space: O(n), where n is the total number of key-value pairs in the input nested dictionary.

Method 6: Using a recursive function without a separator

Here is an alternative method that uses a recursive function without a separator. It works by iterating over the items of the dictionary and checking whether each value is another dictionary. If it is, the function calls itself recursively and concatenates the key with the new keys returned by the recursive call. If it is not, the function simply adds the key-value pair to the result dictionary.

Here’s the step by step approach:

  1. Define a function called flatten_dict_recursive that takes a dictionary d as input.
  2. Initialize an empty dictionary called result.
  3. For each key-value pair in d, do the following:
    a. If the value is a dictionary, call flatten_dict_recursive with the value and concatenate the returned keys with the current key.
    b. If the value is not a dictionary, add the current key and value to result.
  4. Return result.

Python3




def flatten_dict(dd, separator='_', prefix=''):
    stack = [(dd, prefix)]
    flat_dict = {}
     
    while stack:
        cur_dict, cur_prefix = stack.pop()
        for key, val in cur_dict.items():
            new_key = cur_prefix + separator + key if cur_prefix else key
            if isinstance(val, dict):
                stack.append((val, new_key))
            else:
                flat_dict[new_key] = val
                 
    return flat_dict
 
# initialising_dictionary
ini_dict = {'geeks': {'Geeks': {'for': 7}},
            'for': {'geeks': {'Geeks': 3}},
            'Geeks': {'for': {'for': 1, 'geeks': 4}}}
 
# printing initial dictionary
print("initial_dictionary", str(ini_dict))
 
# printing final dictionary
print("final_dictionary", str(flatten_dict(ini_dict, separator='')))


Output

initial_dictionary {'geeks': {'Geeks': {'for': 7}}, 'for': {'geeks': {'Geeks': 3}}, 'Geeks': {'for': {'for': 1, 'geeks': 4}}}
final_dictionary {'Geeksforfor': 1, 'Geeksforgeeks': 4, 'forgeeksGeeks': 3, 'geeksGeeksfor': 7}

Time complexity: The time complexity of this function is O(n), where n is the total number of key-value pairs in the dictionary. 

Auxiliary space: The auxiliary space complexity of this function is O(n), where n is the total number of key-value pairs in the dictionary. 



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, a dictionary (dct) and a list (lst). This functi
8 min read
Why does a nested loop perform much faster than the flattened one?
Python provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition checking time. In this article, we will see why does a nested loop performs better than the flattened one. But first, let's see what is a nested loop and what is a flattened loop.4 A nested loop perfo
5 min read
Create a contiguous flattened NumPy array
Let us see how to create a contiguous array in NumPy.The contiguous flattened array is a two-dimensional and multi-dimensional array that is stored as a one-dimensional array. We will be using the ravel() method to perform this task. Syntax : numpy.ravel(array, order = 'C') Parameters : array : Input array. order : C-contiguous, F-contiguous, A-con
2 min read
Compute the median of the flattened NumPy array
In this article, we will discuss how to compute the median of the flattened array. Median is basically that value that separates the lower half of the array with the higher half of array. Example: If there are odd numbers in an array. A = [1,2,3,4,5,6,7] Then the median element will be 7+1/2= 4th element of the array. Hence the Median in this array
2 min read
PyCairo - How we can copy flattened path?
In this article we will learn how we can copy flattened path of object using PyCairo in python. This function is like copy_path() method except that any curves in the path will be approximated with piecewise-linear approximations. PyCairo : Pycairo is a Python module providing bindings for the cairo graphics library.This library is used for creatin
3 min read
Python | Convert list of nested dictionary into Pandas dataframe
Given a list of the nested dictionary, write a Python program to create a Pandas dataframe using it. We can convert list of nested dictionary into Pandas DataFrame. Let's understand the stepwise procedure to create a Pandas Dataframe using the list of nested dictionary. Convert Nested List of Dictionary into Pandas DataframeBelow are the methods th
4 min read
Python | Convert nested sublist into tuples
Sometimes, while working with huge amount of data, we might have in which each point of data constitutes of several elements and needs to be processed as single unit. For this, we might sometimes, just receive a matrix and it's constituent data points are also lists, which don't make much sense and whole and might be required to be converted to tup
8 min read
Python | Convert given list into nested list
Sometimes, we come across data that is in string format in a list and it is required to convert it into a list of the list. This kind of problem of converting a list of strings to a nested list is quite common in web development. Let's discuss certain ways in which this can be performed. Convert the Given List into Nested List in PythonBelow are th
5 min read
Python - Convert Nested Tuple to Custom Key Dictionary
Sometimes, while working with Python records, we can have data that come without proper column names/identifiers, which can just be identified by their index, but we intend to assign them keys and render in form of dictionaries. This kind of problem can have applications in domains such as web development. Let's discuss certain ways in which this t
4 min read
Python - Convert Nested dictionary to Mapped Tuple
Sometimes, while working with Python dictionaries, we can have a problem in which we need to convert nested dictionaries to mapped tuple. This kind of problem can occur in web development and day-day programming. Let's discuss certain ways in which this task can be performed. Input : test_dict = {'gfg' : {'x' : 5, 'y' : 6, 'z': 3}, 'best' : {'x' :
4 min read
Convert nested Python dictionary to object
Let us see how to convert a given nested dictionary into an object Method 1 : Using the json module. We can solve this particular problem by importing the json module and use a custom object hook in the json.loads() method. C/C++ Code # importing the module import json # declaringa a class class obj: # constructor def __init__(self, dict1): self.__
2 min read
How to convert a MultiDict to nested dictionary using Python
A MultiDict is a dictionary-like object that holds multiple values for the same key, making it a useful data structure for processing forms and query strings. It is a subclass of the Python built-in dictionary and behaves similarly. In some use cases, we may need to convert a MultiDict to a nested dictionary, where each key corresponds to a diction
3 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 convert flat dictionaries into nested dictionaries. Conv
4 min read
Python - Convert Lists to Nested Dictionary
Sometimes, while working with Python dictionaries, we can have a problem in which we need to convert lists to nestings, i.e. each list value represents a new nested level. This kind of problem can have applications in many domains including web development. Let's discuss the certain way in which this task can be performed. Convert Lists to Nested D
5 min read
Convert Nested Dictionary to List Python
Python dictionaries are versatile data structures that allow the storage of key-value pairs. However, working with nested dictionaries, where values themselves are dictionaries, can sometimes be challenging. In this article, we will explore various simple and commonly used methods to convert a nested dictionary into a list in Python. Example: Input
3 min read
Python | Check if a nested list is a subset of another nested list
Given two lists list1 and list2, check if list2 is a subset of list1 and return True or False accordingly. Examples: Input : list1 = [[2, 3, 1], [4, 5], [6, 8]] list2 = [[4, 5], [6, 8]] Output : True Input : list1 = [['a', 'b'], ['e'], ['c', 'd']] list2 = [['g']] Output : False Let's discuss few approaches to solve the problem. Approach #1 : Naive
7 min read
Python program to convert Set into Tuple and Tuple into Set
Let's see how to convert the set into tuple and tuple into the set. For performing the task we are use some methods like tuple(), set(), type(). tuple(): tuple method is used to convert into a tuple. This method accepts other type values as an argument and returns a tuple type value.set(): set method is to convert other type values to set this meth
7 min read
Python | Convert a list of Tuples into Dictionary
Sometimes you might need to convert a tuple to dict object to make it more readable. In this article, we will try to learn how to convert a list of tuples into a dictionary. Here we will find two methods of doing this. Examples: Input : [("akash", 10), ("gaurav", 12), ("anand", 14), ("suraj", 20), ("akhil", 25), ("ashish", 30)] Output : {'akash': [
6 min read
Python | Convert dictionary object into string
The dictionary is an important container and is used almost in every code of day-to-day programming as well as web development with Python. The more it is used, the more is the requirement to master it and hence it's necessary to learn about them. Input: { "testname" : "akshat","test2name" : "manjeet","test3name" : "nikhil"}Output: {"testname": "ak
3 min read
Python | Convert a set into dictionary
Sometimes we need to convert one data structure into another for various operations and problems in our day to day coding and web development. Like we may want to get a dictionary from the given set elements.Let's discuss a few methods to convert given set into a dictionary. Method #1: Using fromkeys() C/C++ Code # Python code to demonstrate # conv
2 min read
Python | Convert key-value pair comma separated string into dictionary
Given a string, with different key-value pairs separated with commas, the task is to convert that string into the dictionary. These types of problems are common in web development where we fetch arguments from queries or get a response in the form of strings. Given below are a few methods to solve the task. Method #1: Using dictionary comprehension
5 min read
Python | Convert list of tuple into dictionary
Given a list containing all the element and second list of tuple depicting the relation between indices, the task is to output a dictionary showing the relation of every element from the first list to every other element in the list. These type of problems are often encountered in Coding competition. Below are some ways to achieve the above task. I
8 min read
Python program to Convert a list into a dictionary with index as key
Given a python list, the task is to write a Python program to convert the list into a dictionary with indexes as keys. Example: Input - ['Ramesh', 'Mahesh', 'Kamlesh', 'Suresh']Output - {0: 'Ramesh', 1: 'Mahesh', 2: 'Kamlesh', 3: 'Suresh'} Input - ['Carrot', 'Raddish', 'Brinjal', 'Potato']Output - {0: 'Carrot', 1: 'Raddish', 2: 'Brinjal', 3: 'Potat
5 min read
Convert List Of Dictionary into Python String
Working with data structures like lists of dictionaries is a common task in Python programming. Sometimes, you may need to convert a list of dictionaries into a string for various reasons, such as storing it in a file, sending it over the network, or displaying it in a specific format. In this article, we will explore three simple and commonly used
3 min read
Python | Convert two lists into a dictionary
Interconversion between data types is usually necessary for real-time applications as certain systems have certain modules that require input in a particular data type. Let's discuss a simple yet useful utility of conversion of two lists into a key: value pair dictionary in Python. Converting Two Lists into a DictionaryPython List is a sequence dat
11 min read
Convert String Dictionary to Dictionary Python
Interconversions of data types have been discussed many times and have been quite a popular problem to solve. This article discusses yet another problem of interconversion of the dictionary, in string format to a dictionary. Let's discuss certain ways in which this can be done. Convert String Dictionary to Dictionary Using json.loads() This task ca
6 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 which this task can be performed. Input : test_list =
9 min read
Python | Split nested list into two lists
Given a nested 2D list, the task is to split the nested list into two lists such that the first list contains the first elements of each sublist and the second list contains the second element of each sublist. In this article, we will see how to split nested lists into two lists in Python. Python Split Nested List into Two ListsBelow are the ways b
5 min read
Python Nested Dictionary
A Dictionary in Python works similarly to the Dictionary in the real world. The keys of a Dictionary must be unique and of immutable data types such as Strings, Integers, and tuples, but the key values can be repeated and be of any type. What is Python in Nested Dictionary? Nesting Dictionary means putting a dictionary inside another dictionary. Ne
3 min read
Python | Sum values for each key in nested dictionary
Given a nested dictionary and we have to find sum of particular value in that nested dictionary. This is basically useful in cases where we are given a JSON object or we have scraped a particular page and we want to sum the value of a particular attribute in objects. Code #1: Find sum of sharpness values using sum() function Step-by-step approach:
2 min read