Hello,
I am trying to read data from a dictionary. I have a dictionary with information about familes and I am trying to collect the names of all the fathers from it. But I am facing an issue as described below.
my_dict = {"family": {"Father":"Sami", "Mother":"Jami", "Kids":1}, "family" : {"Father":"Jeebo", "Mother":"Freebo", "Kids":2}, "family" : {"Father":"Krato", "Mother":"Mrato", "Kids":2}}
father_names = []
for val in my_dict:
father.append(my_dict[val]["Father"])
print(father)
Output :
['Krato']
What I expected was to get the list updated with three names, atleast with all same names that means as [‘Krato’, ‘Krato’, ‘Krato’] for example. But it looks like looping is not happening.
If I replace the key name family with family1, family2 and family3 respectively, then I am getting three different names appended in the list as shown below. That means looping is happening. This is clear.
my_dict = {"family1": {"Father":"Sami", "Mother":"Jami", "Kids":1}, "family2" : {"Father":"Jeebo", "Mother":"Freebo", "Kids":2}, "family3" : {"Father":"Krato", "Mother":"Mrato", "Kids":2}}
father_names = []
for val in my_dict:
father.append(my_dict[val]["Father"])
print(father)
Output :
['Sami', 'Jeebo', 'Krato']
I am not understanding why looping is not happening in the first case where the family is the key name.
Thanks in advance for the help…