Nested Dictionary in PythonIn the Python programming language, we have the concept of dictionaries. The dictionaries are mutable, and we can easily add and delete the items from the dictionary. It is a collection of unordered data items.
Syntax of Nested dictionary for adding the various dictionaries into a particular one: Adding 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, Nesteddict[dict][key] = ‘value’. Another way is to add the whole dictionary in one go, Nesteddict[dict] = { ‘key’: ‘value’}. Examples of Nested DictionaryLet us understand with the help of some examples: Example 1: Explanation: In the above example, we created a dictionary containing the integer key values with the corresponding string values. Here we have symbolized with the student’s data in the class having roll number with the corresponding name of each student. Further, we will perform the nested operation inside this dictionary. The Output of the following Program Printing the dictionary that contains integer keys with their corresponding values {1: 'Rina', 2: 'Gita', 3: 'Sita'} Example 2: Explanation: We have created a dictionary that does not contain any of the keys with the corresponding values. Further, we will perform the nested operation inside this dictionary. The Output of the following Program Simple empty dictionary: { } Example 3: Explanation: We have created a nested dictionary, which contains the empty data set, or an empty dictionary that does not contain any data item with their corresponding key values. The Output of the following Program Nested dictionary are as follows - {'dict1': {}, 'dict2': {}, 'dict3': {}} Example 4: Explanation: In the above example, we created a dictionary containing the string key values and corresponding integer values. Here we have symbolized with the student’s data in the class having Grade with the corresponding roll number of each student. Further, we will perform the nested operation inside this dictionary. The Output of the following Program Printing the dictionary that contains string keys with their corresponding integer values {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5} Example 5: Explanation: We have created a dictionary containing the data items in the form of pairs. We have created a list of paired items and made them a dictionary. The Output of the following Program Dictionary with each item as a pair: {1: 'silk', 2: 'one'} Example 6: Explanation: We have created three separate dictionaries and assigned the elements at the corresponding key values one by one. This dictionary contains the integer key values with the corresponding string values. Still, we have created it separately, and after that, we have added these dictionaries. Thus, we can perform the addition operation in the dictionary. We will also perform the nested operation inside this dictionary. The Output of the following Program Dictionary after adding 3 elements: {1: 'Java', 2: 'Tpoint', 3: 1} Example 7: Explanation: We have created a dictionary, which contains the integer key values with the corresponding string values. Here, we have performed the update and the adding operation in the dictionary. We have also made some changes in it and converted it into a nested dictionary. The Output of the following Program Dictionary after adding 3 elements: {'Name': 'JavaTpoint', 1: [11, 12, 13], 'Value': (5, 3, 6)} Updated key value: {'Name': 'JavaTpoint', 1: [11, 12, 13], 'Value': (5, 3, 6), 2: 'JavaTpoint'} Adding a Nested Key: {'Name': 'JavaTpoint', 1: [11, 12, 13], 'Value': (5, 3, 6), 2: 'JavaTpoint', 5: {'Nested': {'5': 'Java', '3': 'T'}}} Example 8: Explanation: We have created a nested dictionary that contains the key values with the corresponding values. Here, we have used the concept of mixed keys, in which keys are not the same. We will extend it and make a nested dictionary with the same keys but different values. The Output of the following Program Dictionary with the use of Mixed Keys: {'Name': 'JavaTpoint', 1: [11, 12, 13]} Example 9: Explanation: In the above example, we have created a nested dictionary that contains the key values with the corresponding values, here we have used the concept of the same keys, in which keys are the same, but the corresponding data values are different. The Output of the following Program Nested dictionary 2- {'Dict1': {'Name': 'Reena', 'age': '22'}, 'Dict2': {'Name': 'Jatin', 'age': '19'}} Example 10: Explanation: We have created a nested dictionary that contains the integer key values with the corresponding string values. Here, we have first printed the nested dictionary and an empty nested dictionary. We have made some changes and placed the nested dictionary inside the empty dictionary. We have also added the two nested dictionaries. The Output of the following Program Nested dictionary 3- {'Dict1': {1: 'J', 2: 'T', 3: 'P'}, 'Dict2': {'Name': 'JTP', 1: [1, 2]}} Initial nested dictionary:- {} After adding dictionary Dict1 {'Dict1': {'name': 'Boby', 'age': 21}} Let us take a fix example and then see some variations in it, so that we can easily understand it easily: Example 11: Explanation: Here we have created a simple nested dictionary; further, we will make some changes. The Output of the following Program {1: {'name': 'Shivam', 'age': '22', 'Id': 10023}, 2: {'name': 'Anjali', 'age': '20', 'Id': 10024}} Example 12: Explanation: Here we have created a nested dictionary and use the [ ] syntax to access the elements from the dictionary, here it is used when we supply the name of the dictionary than in this [ ] square brackets position of the element you want to fetch, and after than in additional [ ] square brackets supply the attribute or the key value that you want to fetch for the particular element. The Output of the following Program Shivam 22 10023 Example 13: Explanation: Here we have created a nested dictionary, and we want to add more elements to that dictionary. It is all done by using [ ] square brackets syntax, firstly we have created an empty set at position 3 in the dictionary, after than one by one we have filled the data into it, here it is used when we supply the name of the dictionary then in this [ ] square brackets position of the element you want to add and after than in additional [ ] square brackets supply the attribute or the key value that you want to assign for the particular element using the equals to sign. The Output of the following Program {'name': 'Tina', 'age': '19', 'Id': '10034'} Example 14: Explanation: Here we have created a nested dictionary, and we want to add more elements to that dictionary. It is all done by using [ ] square brackets syntax. At first, we have created an empty set at position 3 in the dictionary, after that one by one we have filled the data into it. Here, it is used when we supply the name of the dictionary then in this [ ] square brackets position of the element you want to add and after than in additional [ ] square brackets supply the attribute or the key value that you want to assign for the particular element using the equals to sign. Now for deleting the particular element, let say the id of student 3 from the nested dictionary; we have to use the ‘ del ‘ keyword in it; by using it, we can easily delete the particular value that we want. The Output of the following Program {'name': 'Tina', 'age': '19', 'Id': '10034'} {'name': 'Tina', 'age': '19'} Example 15: Explanation: Here we have created a nested dictionary, and we want to add more elements to that dictionary. It is all done by using [ ] square brackets syntax. At first, we have created an empty set at position 3 in the dictionary, after that one by one we have filled the data into it, here it is used when we supply the name of the dictionary then in this [ ] square brackets position of the element you want to add and after than in additional [ ] square brackets supply the attribute or the key value that you want to assign for the particular element using the equals to sign. For deleting the particular dictionary inside the nested dictionary, we have used the ‘ del ‘ keyword and deleted the whole dictionary of student 3 from the student nested dictionary. The Output of the following Program {'name': 'Tina', 'age': '19', 'Id': '10034'} {1: {'name': 'Shivam', 'age': '22', 'Id': 10053}, 2: {'name': 'Anjali', 'age': '20', 'Id': 10004}} |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263300.html