"""I modified the family keys here again two have 3 distinct key-value pairs"""
my_list = [{
"family1": {"Father":"Sami", "Mother":"Jami", "Kids":1},
"family2": {"Father":"Jeebo", "Mother":"Freebo", "Kids":2},
"family3": {"Father":"Krato", "Mother":"Mrato", "Kids":2}
}]
my_list
[{'family1': {'Father': 'Sami', 'Mother': 'Jami', 'Kids': 1}, 'family2': {'Father': 'Jeebo', 'Mother': 'Freebo', 'Kids': 2}, 'family3': {'Father': 'Krato', 'Mother': 'Mrato', 'Kids': 2}}]
"""
There is only one element in my_lis. But if we observe the element, it is nested twice:
1. internal dictionary = {"Father":"Sami", "Mother":"Jami", "Kids":1} - having 3 keys and 3 values
2. external dictionary = {family1 : {internal dictionary}} - having 1 key and 1 value
"""
print("length of my_list:", len(my_list))
length of my_list: 1
# here we access one row at a time from my_list (in this case we have only 1)
for each_row in my_list:
# 1. Since each_row now is dictionary, we unpack the key and value from it
for dict_key, dict_values in each_row.items():
# we print each key of the external dictionary and chain this key, with one of the keys from interal dictionary
print("external Key =", dict_key, "|internal key = Father |", "internal value = ", each_row[dict_key]["Father"])
external Key = family1 |internal key = Father | internal value = Sami external Key = family2 |internal key = Father | internal value = Jeebo external Key = family3 |internal key = Father | internal value = Krato
Let's complicate the 'my_list' by adding some more levels of nesting:
Here is where the real deal is! Each of element in the list represent a different dictionary, unlike your example where, single dictionary contained three keys with the same name. So python accepts these 3 different dictionries with the same key name and stores them as displayed in result below
nested_dict = {
"family_row":
{
"Address":
[{
"family":
{
"Father":"Sami", "Mother":"Jami", "Kids":1, "street": "alpha", "pin": 11111
}
},
{
"family":
{
"Father":"Jeebo", "Mother":"Freebo", "Kids":2, "street": "betas", "pin": 22222
}
},
{
"family":
{
"Father":"Krato", "Mother":"Mrato", "Kids":2, "street": "gamma", "pin": 33333
}
}]
}
}
nested_dict
{'family_row': {'Address': [{'family': {'Father': 'Sami', 'Mother': 'Jami', 'Kids': 1, 'street': 'alpha', 'pin': 11111}}, {'family': {'Father': 'Jeebo', 'Mother': 'Freebo', 'Kids': 2, 'street': 'betas', 'pin': 22222}}, {'family': {'Father': 'Krato', 'Mother': 'Mrato', 'Kids': 2, 'street': 'gamma', 'pin': 33333}}]}}
Considering the mission, the equivalent for "python_top" dictionary would be nested_dict dictionary.
Let us see what's the equivalent for python_top["data"]["children"] part of the code is
print("type:", type(nested_dict["family_row"]["Address"]), "| len:", len(nested_dict["family_row"]["Address"]))
type: <class 'list'> | len: 3
Hmmm, so nested_dict["family_row"]["Address"] is a list having all three dictionaries. Let's print the results
nested_dict["family_row"]["Address"]
[{'family': {'Father': 'Sami', 'Mother': 'Jami', 'Kids': 1, 'street': 'alpha', 'pin': 11111}}, {'family': {'Father': 'Jeebo', 'Mother': 'Freebo', 'Kids': 2, 'street': 'betas', 'pin': 22222}}, {'family': {'Father': 'Krato', 'Mother': 'Mrato', 'Kids': 2, 'street': 'gamma', 'pin': 33333}}]
Let's see the for loop now and say we want to extract the Street name from each of the Addresses. We will do this in two parts.
# 1.
for each_element in nested_dict["family_row"]["Address"]:
print("element:", each_element, "| type:", type(each_element))
element: {'family': {'Father': 'Sami', 'Mother': 'Jami', 'Kids': 1, 'street': 'alpha', 'pin': 11111}} | type: <class 'dict'> element: {'family': {'Father': 'Jeebo', 'Mother': 'Freebo', 'Kids': 2, 'street': 'betas', 'pin': 22222}} | type: <class 'dict'> element: {'family': {'Father': 'Krato', 'Mother': 'Mrato', 'Kids': 2, 'street': 'gamma', 'pin': 33333}} | type: <class 'dict'>
# 2.
for each_element in nested_dict["family_row"]["Address"]:
for each_key in each_element:
print(each_key, "| street:", each_element[each_key]["street"], "| pin:", each_element[each_key]["pin"])
family | street: alpha | pin: 11111 family | street: betas | pin: 22222 family | street: gamma | pin: 33333
# for each_element in nested_dict["family_row"]["Address"]:
# for each_key in each_element:
# print(each_key)
# for each_element in nested_dict["family_row"]["Address"]:
# for each_key, each_value in each_element.items():
# print(each_key, "| street:", each_value["street"], "| pin:", each_value["pin"])