In Python, a dictionary is an unordered collection of items. For example: Show
dictionary = {'key' : 'value', 'key_2': 'value_2'} Here, dictionary has a To learn more about dictionary, please visit Python Dictionary. What is Nested Dictionary in Python?In Python, a nested dictionary is a dictionary inside a dictionary. It's a collection of dictionaries into one single dictionary. nested_dict = { 'dictA': {'key_1': 'value_1'}, 'dictB': {'key_2': 'value_2'}} Here, the nested_dict is a nested dictionary with the dictionary Create a Nested DictionaryWe're going to create dictionary of people within a dictionary. Example 1: How to create a nested dictionary
When we run above program, it will output: {1: {'name': 'John', 'age': '27', 'sex': 'Male'}, 2: {'name': 'Marie', 'age': '22', 'sex': 'Female'}} In the above program, people is a nested dictionary. The internal dictionary Access elements of a Nested DictionaryTo access element of a nested dictionary, we use
indexing Example 2: Access the elements using the [] syntax
When we run above program, it will output: John 27 Male In the above program, we print the value of key Add element to a Nested DictionaryExample 3: How to change or add elements in a nested dictionary?
When we run above program, it will output: {'name': 'Luna', 'age': '24', 'sex': 'Female', 'married': 'No'} In the above program, we create an empty dictionary Then, we add the Example 4: Add another dictionary to the nested dictionary
When we run above program, it will output: {'name': 'Peter', 'age': '29', 'sex': 'Male', 'married': 'Yes'} In the above program, we assign a dictionary literal to Delete elements from a Nested DictionaryIn Python, we use “ del “ statement to delete elements from nested dictionary. Example 5: How to delete elements from a nested dictionary?
When we run above program, it will output: {'name': 'Luna', 'age': '24', 'sex': 'Female'} {'name': 'Peter', 'age': '29', 'sex': 'Male'} In the above program, we delete the Example 6: How to delete dictionary from a nested dictionary?
When we run above program, it will output: {1: {'name': 'John', 'age': '27', 'sex': 'Male'}, 2: {'name': 'Marie', 'age': '22', 'sex': 'Female'}} In the above program, we delete both the internal dictionary
Iterating Through a Nested DictionaryUsing the for loops, we can iterate through each elements in a nested dictionary. Example 7: How to iterate through a Nested dictionary?
When we run above program, it will output: Person ID: 1 Name: John Age: 27 Sex: Male Person ID: 2 Name: Marie Age: 22 Sex: Female In the above program, the first loop returns all the keys in the nested dictionary people. It consist of the IDs p_id of each person. We use these IDs to unpack the information p_info of each person. The second loop goes through the information of each person. Then, it returns all of the keys Now, we print the key of the person’s information and the value for that key. Key Points to Remember:
How to rename dictionary keys in Python?Use dict.. a_dict = {"a": 1, "B": 2, "C": 3}. new_key = "A". old_key = "a". a_dict[new_key] = a_dict. pop(old_key). print(a_dict). How to add items in nested dictionary in Python?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'}.
How to access nested dictionary in list Python?You can make use of the eval function in python. nestq will be "nest['b']['v']['y']" where nest is the nested dictionary. The eval builtin function executes the given string.
How to access value in nested dictionary?You can access individual items in a nested dictionary by specifying key in multiple square brackets. If you refer to a key that is not in the nested dictionary, an exception is raised. To avoid such exception, you can use the special dictionary get() method.
|