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

Advertisements

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?

>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.

Leave a ReplyCancel reply