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. Nesting is of great use as the kind of information we can model in programs is expanded greatly.
nested_dict = {'dict1': {'key_A': 'value_A'},
'dict2': {'key_B': 'value_B'}}
Example
Python3
Dict = { 1 : 'Geeks' , 2 : 'For' , 3 : { 'A' : 'Welcome' , 'B' : 'To' , 'C' : 'Geeks' }}
|
Illustration using Image
Creating a Nested Dictionary
In Python, a Nested dictionary can be created by placing the comma-separated dictionaries enclosed within braces.
Python3
Dict = { 'Dict1' : { },
'Dict2' : { }}
print ( "Nested dictionary 1-" )
print ( Dict )
Dict = { 'Dict1' : { 'name' : 'Ali' , 'age' : '19' },
'Dict2' : { 'name' : 'Bob' , 'age' : '25' }}
print ( "\nNested dictionary 2-" )
print ( Dict )
Dict = { 'Dict1' : { 1 : 'G' , 2 : 'F' , 3 : 'G' },
'Dict2' : { 'Name' : 'Geeks' , 1 : [ 1 , 2 ]} }
print ( "\nNested dictionary 3-" )
print ( Dict )
|
Output:
Nested dictionary 1-
{'Dict1': {}, 'Dict2': {}}
Nested dictionary 2-
{'Dict1': {'name': 'Ali', 'age': '19'}, 'Dict2': {'name': 'Bob', 'age': '25'}}
Nested dictionary 3-
{'Dict1': {1: 'G', 2: 'F', 3: 'G'}, 'Dict2': {1: [1, 2], 'Name': 'Geeks'}}
Adding Elements to a Nested Dictionary
The addition of elements to a nested Dictionary can be done in multiple ways. One way to add a dictionary in the Nested dictionary is to add values one be one, Nested_dict[dict][key] = ‘value’. Another way is to add the whole dictionary in one go, Nested_dict[dict] = { ‘key’: ‘value’}.
Python3
Dict = { }
print ( "Initial nested dictionary:-" )
print ( Dict )
Dict [ 'Dict1' ] = {}
Dict [ 'Dict1' ][ 'name' ] = 'Bob'
Dict [ 'Dict1' ][ 'age' ] = 21
print ( "\nAfter adding dictionary Dict1" )
print ( Dict )
Dict [ 'Dict2' ] = { 'name' : 'Cara' , 'age' : 25 }
print ( "\nAfter adding dictionary Dict1" )
print ( Dict )
|
Output:
Initial nested dictionary:-
{}
After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}}
After adding dictionary Dict1
{'Dict1': {'age': 21, 'name': 'Bob'}, 'Dict2': {'age': 25, 'name': 'Cara'}}
Access Elements of a Nested Dictionary
In order to access the value of any key in the nested dictionary, use indexing [] syntax.
Python3
Dict = { 'Dict1' : { 'name' : 'Ali' , 'age' : '19' },
'Dict2' : { 'name' : 'Bob' , 'age' : '25' }}
print ( Dict [ 'Dict1' ][ 'name' ])
print ( Dict [ 'Dict2' ][ 'age' ])
|
Output:
Ali
25
Deleting Dictionaries from a Nested Dictionary
Deletion of dictionaries from a nested dictionary can be done either by using the Python del keyword or by using pop() function.
Python3
Dict = { 'Dict1' : { 'name' : 'Ali' , 'age' : 19 },
'Dict2' : { 'name' : 'Bob' , 'age' : 21 }}
print ( "Initial nested dictionary:-" )
print ( Dict )
print ( "\nDeleting Dict2:-" )
del Dict [ 'Dict2' ]
print ( Dict )
print ( "\nDeleting Dict1:-" )
Dict .pop( 'Dict1' )
print ( Dict )
|
Output:
Initial nested dictionary:-
{'Dict2': {'name': 'Bob', 'age': 21}, 'Dict1': {'name': 'Ali', 'age': 19}}
Deleting Dict2:-
{'Dict1': {'name': 'Ali', 'age': 19}}
Deleting Dict1:-
{}
Similar Reads
Update Nested Dictionary - Python
A nested dictionary in Python is a dictionary that contains another dictionary (or dictionaries) as its value. Updating a nested dictionary involves modifying its structure or content by: Adding new key-value pairs to any level of the nested structure.Modifying existing values associated with specif
4 min read
Python Dictionary Methods
Python dictionary methods is collection of Python functions that operates on Dictionary. Python Dictionary is like a map that is used to store data in the form of a key: value pair. Python provides various built-in functions to deal with dictionaries. In this article, we will see a list of all the f
5 min read
Python Dictionary Exercise
Basic Dictionary ProgramsPython | Sort Python Dictionaries by Key or ValueHandling missing keys in Python dictionariesPython dictionary with keys having multiple inputsPython program to find the sum of all items in a dictionaryPython program to find the size of a DictionaryWays to sort list of dicti
3 min read
Python Dictionary keys() method
keys() method in Python dictionary returns a view object that displays a list of all the keys in the dictionary. This view is dynamic, meaning it reflects any changes made to the dictionary (like adding or removing keys) after calling the method. Example: [GFGTABS] Python d = {'A': 'Geek
2 min read
Dictionary has_key() - Python
The has_key() method in Python was used to check whether a specified key exists in a dictionary. However, this method was removed in Python 3 and the preferred approach to check for a key's existence is by using the in keyword. In Python 2.7, the has_key() method was commonly used, like so: [GFGTABS
3 min read
Python Dictionary items() method
items() method in Python returns a view object that contains all the key-value pairs in a dictionary as tuples. This view object updates dynamically if the dictionary is modified. Example: [GFGTABS] Python d = {'A': 'Python', 'B': 'Java', 'C': 'C++'} #
2 min read
Python - Nested dictionary Combinations
Sometimes, while working with Python dictionaries, we can have a problem in which we need to construct all the combination of dictionary keys with different values. This problem can have application in domains such as gaming and day-day programming. Lets discuss certain way in which we can perform t
3 min read
Dictionaries in Python
A Python dictionary is a data structure that stores the value in key: value pairs. Values in a dictionary can be of any data type and can be duplicated, whereas keys can't be repeated and must be immutable. Example: Here, The data is stored in key:value pairs in dictionaries, which makes it easier t
5 min read
Python Dictionary fromkeys() Method
Python dictionary fromkeys() function returns the dictionary with key mapped and specific value. It creates a new dictionary from the given sequence with the specific value. Python Dictionary fromkeys() Method Syntax: Syntax : fromkeys(seq, val) Parameters : seq : The sequence to be transformed into
3 min read
Iterate over a dictionary in Python
In this article, we will cover How to Iterate Through a Dictionary in Python. To Loop through values in a dictionary you can use built-in methods like values(), items() or even directly iterate over the dictionary to access values with keys. How to Loop Through a Dictionary in PythonThere are multip
6 min read