Help : Reading from dictionary


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:

Output :


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:

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…

1 Like

hey @sreekanthac

Imagine all of us are named “@sreekanthac” in the DQ forum. Mary, April.G, Bruno, Nityesh, me, and you. And this happens:

sreekanthac posted this so sreekanthac answered this which didn’t make sense to sreekanthac so sreekanthac reposted that and sreekanthac jumped the discussion then finally sreekanthac had to interrupt and make everyone sit in a different corner!

6 humans can’t make a difference, how can we expect a dictionary to do that? :thinking:

When you have the same “key” names, the dictionary takes the last key-value pair and overrides the former one, so only the last one gets stored/ created. Since python is case-sensitive the my_dict_2 works too like my_dict_1 (I renamed it for readability), however my_dict stores only one key-value pair.


Hey @Rucha,
Thank you very much for the quick reply. Well, I too had the same understanding. But I got confused while working on the below mission. In this mission we are reading json data which is converted to a python object, in this case to a dictionary using .json() method. We are saving this .json() converted dictionary to a variable python_top. If you analyse the variable python_top, you will observe a key called children which inturn saves information as a list. And inside this list is a dictionary where the key ‘data’ is repeatedly used. So I was not able to understand how can be this possible.

I cannot copy and paste the variable python_top data here since it is quite huge. But you can check the below link for more details.

Hello @sreekanthac, I just finished this mission and I think I can help here.

So, we’ve got this variable python_top_articles that is a list of posts in the python_top variable. We created it with the following code:

python_top_articles = python_top['data']['children']

What we do next is to loop over this variable using a for. So we have:

for article in python_top_articles:

We then assign the key 'data' in this dictionary to the variable ar, like this:

    ar = article['data']

Notice that each article is a different dictionary that stores data about a different post on reddit and that is contained in the python_top_articles list, so each time the loop goes to next article it goes to a new dictionary. What we are doing then, is looking for the key 'data' in several different dictionaries, not going through a single dictionary looking for several keys named 'data'.

I hope this helps you.


Hello @otavios.s. Thank you for the quick response. Good to know you have just finished this mission, as that makes things easier.

As you said the variable python_top_articles which is a list stores data in the form of dictionary. And for each of this dictionary the key is ‘data’. How is this possible was my question?
I will try to explain my question wrt to above family example I used.

my_list = [{"family": {"Father":"Sami", "Mother":"Jami", "Kids":1}, "family": {"Father":"Jeebo", "Mother":"Freebo", "Kids":2}, 
             "family": {"Father":"Krato", "Mother":"Mrato", "Kids":2}}]

[{'family': {'Father': 'Krato', 'Mother': 'Mrato', 'Kids': 2}}]

As you can see here the list is saving only one information because of key duplication.
I think we can replace my_list with python_top_articles and the key family with data in the real example. How is key data duplication not an issue in our real example?


It is possible since nothing stops different dictionaries from having a key with the same name. The python_top_articles is a list filled with several different dictionaries, each one with its own 'data' key.

In your example, you have only one dictionary with three keys with the same name, 'family'. In this case, as @Rucha said, and I quote:


  • python_top_articles: List with several independent dictionaries, each one with its own 'data' key.

  • my_list: List with only one dictionary that has three keys with the same name, 'family'.


hi @sreekanthac

In case you have understood the explanation given by @@otavios.s, kindly ignore this.
And if you do refer to this notebook, and are still in doubt do let us know.

Nested_Dictionaries.ipynb (10.4 KB)

Click here to view the jupyter notebook file in a new tab


Hi @otavios.s and @Rucha :slightly_smiling_face:, Thank you both of you for providing the answers, esp @Rucha for taking time to provide such a detailed explanation. It was my bad that I did not understand or rather try to understand the variable python_top_articles correclty, particulary because of its size. My initial understanding was that python_top_articles was storing data like in my_list1(only one dictionary), where as the in reality it was like in my_list2 (three dictionaries). This resulted in all these confusions :roll_eyes:.

my_list1 = [{"family": {"Father":"Sami", "Mother":"Jami", "Kids":1}, 
             "family": {"Father":"Jeebo", "Mother":"Freebo", "Kids":2}, 
             "family": {"Father":"Krato", "Mother":"Mrato", "Kids":2}}] 

my_list2 = [{"family": {"Father":"Sami", "Mother":"Jami", "Kids":1}}, 
            {"family": {"Father":"Jeebo", "Mother":"Freebo", "Kids":2}}, 
            {"family": {"Father":"Krato", "Mother":"Mrato", "Kids":2}}] 

Since the system allows me to mark only one asnwer as solution, I will mark @Rucha as solution esp for the effort you put in to explain :grinning: . Having said that @otavios.s answer was helpful enough to understand my mistake.

1 Like

Hey I also think you should mark @Rucha’s as solution, you didn’t even need to explain :sweat_smile:

@Rucha was already doing a good job answering your question, I just felt like I could help because I had finished that mission like 5 minutes before reading your topic, so I knew exactly what you were talking about.

And I’m glad I got it.

1 Like

hey @sreekanthac and @otavios.s

Thank you for the compliments. :slight_smile: The important take away here is we were able to solve a problem/ doubt collectively!

The only big deal of marking a solution is that the post then helps other students. I guess it helps during the search of topics in the community, I am not 100% sure though.

Thank you forb your efforts too @otavios.s :+1: You are doing great!

1 Like


I know this problem has been solved, but for “additional” perspective, please remember that …

  • Lists allow duplicates.
  • Sets and dictionaries do not.
  • A dictionary is like a set whose items key:value pairs.

my_dict = {“family”: {“Father”:“Sami”, “Mother”:“Jami”, “Kids”:1}, “family” : {“Father”:“Jeebo”, “Mother”:“Freebo”, “Kids”:2}, “family” : {“Father”:“Krato”, “Mother”:“Mrato”, “Kids”:2}}

As initialized above, “my_dict” is a dictionary of dictionaries AND the looping happened.
However, BEFORE the looping, the inner dictionaries was “resolved” into one (1) dictionary to avoid duplicates – given that they have the same key, :family: This is similar to what happens when a set initialized as
my_set = { 1, 2, 3, 4, 2, 3} is “resolved” as my_set{1, 2, 3, 4)

You will see this if you display/print() “my_dict” variable immediately after initializing it. As in ,

  • print(my_dict)

On the other hand, if you remove the enclosing curly brackets “{}” and initialize “my_dict” as shown below, it becomes a list of 3 dictionaries which does’t care if the items are duplicates…

my_dict = [ “family”: {“Father”:“Sami”, “Mother”:“Jami”, “Kids”:1}, “family” : {“Father”:“Jeebo”, “Mother”:“Freebo”, “Kids”:2}, “family” : {“Father”:“Krato”, “Mother”:“Mrato”, “Kids”:2}]

Others have done a much better job of explaining this but, I hope this is helpful somehow…

BTW way, you probably meant to write

(Please accept my sincere apologies: I am still learning about the features of the editor used here.)