Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python update a dictionary inside of a dictionary {"Hello":{"type":"greeting"}}

I have the code

dictionary = {"Hello":{}}
tagged = nltk.pos_tag(data)
    for (word, tag) in tagged:
        dictionary[word].update({"type":str(tag)})
print(dictionary)

BTW the "NNP" and "NPP" are just placeholders.
The code is suppose to print {"Hello":{"type":"NNP"}} and it does if it is just one but if it is added to e.g.{"Hello":{},"Goodbye":{}} it changes both {} to {"type":"NPP"}. I have done evaluation and seen that it does change the "tag" but it changes it for all. e.g. for the first (word, tag) in tagged they are both {"type":"NNP"} then on the second one they change to {"type":"NPP"}. How do I stop it from changing all of the values?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem has nothing to do with update or even the fact that the dictionaries are nested; the problem is that you only created a single dictionary that’s used as every value.

Try the following code:

dictionary = {"Hello": {}, "Goodbye": {}}
tagged = [("Hello", "NNP"), ("Goodbye", "NPP")]
for word, tag in tagged:
    dictionary[word].update({"type": tag})
print(dictionary)
# {'Hello': {'type': 'NNP'}, 'Goodbye': {'type': 'NPP'}}

dictionary = dict.fromkeys(["Hello", "Goodbye"], {})
tagged = [("Hello", "NNP"), ("Goodbye", "NPP")]
for word, tag in tagged:
    dictionary[word].update({"type": tag})
print(dictionary)
# {'Hello': {'type': 'NPP'}, 'Goodbye': {'type': 'NPP'}}

In the first block, each value is initialized as a different empty dictionary. When you iterate through and update them, each one is updated individually.

In the second block, each value is intialized as the same empty dictionary. Each time it’s updated, it’s the same dictionary, so the last update overwrites the earlier ones.

If you have a list of words data and you want to create a dictionary of empty dictionaries using the words as keys, do:

dictionary = {word: {} for word in data}

Using a dict comprehension will cause {} to be evaluated once per word (hence creating a new dictionary for each one), whereas in the fromkeys call there is only one singular dictionary provided as an argument.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading