
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Convert flattened dictionary into nested dictionary
Python dictionaries have keys and values. If we have two or more dictionaries to be merged a nested dictionary, then we can take the below approaches. Here year the dictionaries are given along with the new keys that will become a key in the nested dictionary.
Assigning keys
In this approach we will create a new empty dictionary. Then assigned the given dictionaries to each new key. The resulting dictionary will be a nested dictionary with the keys assigned.
Example
dictA = {'Sun': 1, 'Mon': 2} dictB = {'Tue': 3, 'Sun': 5} # Given Dictionaries print("DictA : ",dictA) print("DictB: ",dictB) # Using key access and dict() res = dict() res['Netsed_dict_1'] = dictA res['Netsed_dict_2'] = dictB # printing result print("Netsed Dictionary: \n" ,res)
Running the above code gives us the following result −
Output
DictA : {'Sun': 1, 'Mon': 2} DictB: {'Tue': 3, 'Sun': 5} Netsed Dictionary: {'Netsed_dict_1': {'Sun': 1, 'Mon': 2}, 'Netsed_dict_2': {'Tue': 3, 'Sun': 5}}
Using zip
The Jeep function can convert keys and dictionaries into to a Tuple. Then we apply the dict function to get the final result which is a dictionary containing the new keys as well as the input dictionaries.
Example
dictA = {'Sun': 1, 'Mon': 2} dictB = {'Tue': 3, 'Sun': 5} # Given Dictionaries print("DictA : ",dictA) print("DictB: ",dictB) # Using zip dict_keys = ['Netsed_dict_1','Netsed_dict_2'] all_dicts = [dictA,dictB] res = dict(zip(dict_keys,all_dicts)) # printing result print("Netsed Dictionary: \n" ,res)
Running the above code gives us the following result −
Output
DictA : {'Sun': 1, 'Mon': 2} DictB: {'Tue': 3, 'Sun': 5} Netsed Dictionary: {'Netsed_dict_1': {'Sun': 1, 'Mon': 2}, 'Netsed_dict_2': {'Tue': 3, 'Sun': 5}}
- Related Articles
- Python Convert nested dictionary into flattened dictionary?
- Python - Convert list of nested dictionary into Pandas Dataframe
- Python Pandas - Convert Nested Dictionary to Multiindex Dataframe
- Python - Convert a set into dictionary
- Convert Nested Tuple to Custom Key Dictionary in Python
- Convert dictionary object into string in Python
- Convert two lists into a dictionary in Python
- Convert string dictionary to dictionary in Python
- How to create nested Python dictionary?
- How to convert Javascript dictionary to Python dictionary?
- How I can convert a Python Tuple into Dictionary?
- How to sort a nested Python dictionary?
- Python program to convert a list of tuples into Dictionary
- How to recursively iterate a nested Python dictionary?
- How to count elements in a nested Python dictionary?
