Python | Creating Multidimensional dictionary Last Updated : 27 Apr, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Sometimes, while working with Python dictionaries we need to have nested dictionaries. But the issue is that, we have to declare before initializing a value in nested dictionary. Let's resolve this particular problem via methods discussed in this article. Method #1 : Using setdefault() This function is used to define an empty dictionary on 1st nested level of dictionary to make it 2D. In this case there is no need to define explicit dictionaries at that level of dictionary. Python3 # Python3 code to demonstrate working of # Creating Multidimensional dictionary # Using setdefault() # Initialize dictionary test_dict = {} # printing original dictionary print("The original dictionary : " + str(test_dict)) # Using setdefault() # Creating Multidimensional dictionary test_dict.setdefault(1, {})[4] = 7 # printing result print("Dictionary after nesting : " + str(test_dict)) Output : The original dictionary : {} Dictionary after nesting : {1: {4: 7}} Time Complexity: O(n), where n is the length of the list test_dictAuxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list Method #2 : Using defaultdict() One can achieve the creation of multi nesting using defaultdict(). Not only at one level, but till N level the nesting can be achieved using this. Drawback is that it creates the defaultdict objects. Python3 # Python3 code to demonstrate working of # Creating Multidimensional dictionary # Using defaultdict() from collections import defaultdict # Utility function to create dictionary def multi_dict(K, type): if K == 1: return defaultdict(type) else: return defaultdict(lambda: multi_dict(K-1, type)) # Initialize dictionary test_dict = {} # printing original dictionary print("The original dictionary : " + str(test_dict)) # Using defaultdict() # Creating Multidimensional dictionary # calling function test_dict = multi_dict(3, int) test_dict[2][3][4] = 1 # printing result print("Dictionary after nesting : " + str(dict(test_dict))) Output : The original dictionary : {} Dictionary after nesting : {2: defaultdict(<function multi_dict.<locals>.<lambda> at 0x7f8707a54158>, {3: defaultdict(<class 'int'>, {4: 1})})} Comment More infoAdvertise with us Next Article Create Dictionary from the List-Python M manjeet_04 Follow Improve Article Tags : Python Python Programs Python dictionary-programs Practice Tags : python Similar Reads Create Dynamic Dictionary in Python Creating a Dynamic Dictionary in Python is important in programming skills. By understanding how to generate dictionaries dynamically, programmers can efficiently adapt to changing data requirements, facilitating flexible and responsive code development. In this article, we will explore different me 3 min read Create Dictionary from the List-Python The task of creating a dictionary from a list in Python involves mapping each element to a uniquely generated key, enabling structured data storage and quick lookups. For example, given a = ["gfg", "is", "best"] and prefix k = "def_key_", the goal is to generate {'def_key_gfg': 'gfg', 'def_key_is': 3 min read Create Dictionary Of Tuples - Python The task of creating a dictionary of tuples in Python involves mapping each key to a tuple of values, enabling structured data storage and quick lookups. For example, given a list of names like ["Bobby", "Ojaswi"] and their corresponding favorite foods as tuples [("chapathi", "roti"), ("Paraota", "I 3 min read Python Create Dictionary with Integer The task of creating a dictionary from a list of keys in Python, where each key is assigned a unique integer value, involves transforming the list into a dictionary. Each element in the list becomes a key and the corresponding value is typically its index or a different integer. For example, if we h 3 min read Create Dynamic Dictionary using for Loop-Python The task of creating a dynamic dictionary using a for loop in Python involves iterating through a list of keys and assigning corresponding values dynamically. This method allows for flexibility in generating dictionaries where key-value pairs are added based on specific conditions or inputs during i 3 min read Convert List of Lists to Dictionary - Python We are given list of lists we need to convert it to python . For example we are given a list of lists a = [["a", 1], ["b", 2], ["c", 3]] we need to convert the list in dictionary so that the output becomes {'a': 1, 'b': 2, 'c': 3}. Using Dictionary ComprehensionUsing dictionary comprehension, we ite 3 min read Like