Learn Python Dictionary and How to Create a Nested dictionary?

A nested dictionary is a collection of dictionaries inside a dictionary {key1:{key1:value,key2:value},…..}
Example:
travel_log = { "France": {"cities_visited": ["Paris", "Lille", "Dijon"], "total_visits": 12}, "Germany": {"cities_visited": ["Berlin", "Hamburg", "Stuttgart"], "total_visits": 5}, }
Before discussing Nested Dictionary, Let’s talk about what is a dictionary
Dictionary is one of the data structures used in python to store data as a {key: value} structure and it is denoted as {}
How to declare an empty dictionary
The empty dictionary can be declared with empty curly brackets
empty_dictionary = {} dictionary={"name":"Learn&Share"} print(dictionary)
How to create a nested dictionary
Nesting Dictionary in a Dictionary using static declaration
travel_log = { "France": {"cities_visited": ["Paris", "Lille", "Dijon"], "total_visits": 12}, "Germany": {"cities_visited": ["Berlin", "Hamburg", "Stuttgart"], "total_visits": 5}, } print(travel_log)
Output
{'France': {'cities_visited': ['Paris', 'Lille', 'Dijon'], 'total_visits': 12}, 'Germany': {'cities_visited': ['Berlin', 'Hamburg', 'Stuttgart'], 'total_visits': 5}}
In the above example, We have a collection of dictionaries inside a dictionary
The outer dictionary has -> {“France”:”{“cities_visited”: [“Paris”, “Lille”, “Dijon”], “total_visits”: 12},}
The inner dictionary has -> “cities_visited”: [“Paris”, “Lille”, “Dijon”], “total_visits”: 12
How to create a nested dictionary using the function ()
Code Example:
travel_log = { "France": {"cities_visited": ["Paris", "Lille", "Dijon"], "total_visits": 12}, "Germany": {"cities_visited": ["Berlin", "Hamburg", "Stuttgart"], "total_visits": 5}, } def add_new_country_n(country,visit,cities): dir={"cities_visited":cities,"total_visits":visit} travel_log[country]=dir add_new_country_n("Russia", 2, ["Moscow", "Saint Petersburg"]) print(travel_log)
Output
{'France': {'cities_visited': ['Paris', 'Lille', 'Dijon'], 'total_visits': 12}, 'Germany': {'cities_visited': ['Berlin', 'Hamburg', 'Stuttgart'], 'total_visits': 5}, 'Russia': {'cities_visited': ['Moscow', 'Saint Petersburg'], 'total_visits': 2}}
Explanation:
- The function gets 3 inputs – outer dictionary key, inner dictionary key, and value
- With the inner dictionary{ key: value}, We will create a dictionary
- Then will add the created dictionary to the outer dictionary using the key
How to loop through a nested dictionary
Code Example:
travel_log = { "France": {"cities_visited": ["Paris", "Lille", "Dijon"], "total_visits": 12}, "Germany": {"cities_visited": ["Berlin", "Hamburg", "Stuttgart"], "total_visits": 5}, } for item in travel_log: print(item) print(travel_log[item])
Output:
France {'cities_visited': ['Paris', 'Lille', 'Dijon'], 'total_visits': 12} Germany {'cities_visited': ['Berlin', 'Hamburg', 'Stuttgart'], 'total_visits': 5}
- The outer dictionary key will not be printed by default, So we are printing it separately
- For loop uses the key as an offset value to print the actual value
Check here for more python learning articles. Good Luck with your learning