Python - Dictionary of Dictionaries
A dictionary can be created inside a dictionary, which is also called nested dictionary or dictionary of dictionaries. The below mentioned program describes two ways for creating nested dictionary.
Info = { "person1": { "name": "John", "age": 25, "city": "London" }, "person2": { "name": "Marry", "age": 20, "city": "Newyork" }, "person3": { "name": "Sam", "age": 30, "city": "Paris" } } print(Info["person2"])
The output of the above code will be:
{'name': 'Marry', 'age': 20, 'city': 'Newyork'}
The inner dictionaries can be created outside and combined later with the outer dictionary.
person1 = { "name": "John", "age": 25, "city": "London" } person2 = { "name": "Marry", "age": 20, "city": "Newyork" } person3 = { "name": "Sam", "age": 30, "city": "Paris" } Info = { "person1": person1, "person2": person2, "person3": person3 } print(Info["person2"])
The output of the above code will be:
{'name': 'Marry', 'age': 20, 'city': 'Newyork'}