![]() |
In Python, a dictionary is a built-in data type used to store data in key-value pairs. Defined with curly braces `{}`, each pair is separated by a colon `:`. This allows for efficient representation and easy access to data, making it a versatile tool for organizing information. What is 3 Level Nested Dictionary?A 3-level nested dictionary refers to a dictionary structure in which there are three levels of nesting. In Python, a dictionary is a collection of key-value pairs, and it can contain other dictionaries as values. When you have a dictionary within another dictionary, and then another dictionary within the inner dictionary, you have a 3-level nested dictionary. Example : nested_dict = { 3 Level Nested Dictionary in PythonBelow, are the ways to create 3 Level Nested Dictionary in Python.
3 Level Nested Dictionary Python Direct InitializationIn this example, below code initializes a three-level nested dictionary with a key-value pair at the third level. The Python3
Output
{'first_level_key': {'second_level_key': {'third_level_key': 'value'}}} 3 Level Nested Dictionary Python Using dict() constructorIn this example, below code initializes a three-level nested dictionary using the Python3
Output
{'first_level_key': {'second_level_key': {'third_level_key': 'value'}}} 3 Level Nested Dictionary Python Iterative ApproachIn this example, below code dynamically creates a nested dictionary with the specified levels and assigns the final key-value pair. It then prints the resulting nested dictionary. Python3
Output : {'first_level_key': {'second_level_key': {'third_level_key': {'final_key': 'value'}}}} Wokring with 3 Level Nested Dictionary PythonHere, we will explain how we can access, add, update and delete the element from 3 Level Nested Dictionary Python.
Create 3 Level DictionaryIn this code, Python3
Access ElementIn this example, below code accesses and prints specific information from the nested `contact_details` dictionary, extracting the person’s name, home phone number, and email address. Python3
Output Name: John Doe Add ElementIn this example, below code adds an ‘address’ element to the existing Python3
Output : After Adding Address: Update ElementIn this example, below code updates the ‘work’ phone number in the existing Python3
Output After Updating Work Phone: Delete ElementIn this example, below code deletes the ’email’ element from the existing Python3
Output After Deleting Email: ConclusionNesting dictionaries are powerful for organizing information into hierarchical layers, creating a structured and easily navigable system. This capability proves invaluable when dealing with intricate data, such as organizing student grades within classes, or organizing classes within a school. The flexibility and clarity provided by nested dictionaries enhance the readability and maintainability of code, making it easier for developers to represent real-world relationships in their programs. |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |