Add key and value to a pair of key values ​​Python dictionary

My goal is to add a pair of key values ​​to the value inside the dictionary:

I have the following:

crucial = {'C': {'C': 0, 'B': 1}}
done = {}
for each in crucial:
    for i in each:
        done['D'] = 0
        print(done)
        print(crucial[i].append(done))

Output:

  Traceback (most recent call last):
  File "C:\Users\User\Documents\Programming Full-Stack\Python\Exercise Files\02 Quick Start\conditionals.py", line 13, in <module>
    print(crucial[i].append(done))
AttributeError: 'dict' object has no attribute 'append'
{'D': 0}

Expected Result:

{'C': {'C': 0, 'B': 1, 'D':0}}

Therefore, can someone provide me with guidance on adding a pair of key values ​​to this value field in an external dictionary?

Different approaches: so far I have tried to convert the dictionary to a list declaring d as [], and not with {}. I also tried putting .extend instead of .append. But in none of these cases did I get the result I wanted.

Thank you in advance

+4
source share
2 answers

, dict append. append. , :

d[key] = new_value

new_value , : {'a':1}

, .

d.update(new_stuff)

append, . :

crucial = {'C': {'C': 0, 'B': 1}}
done = {}
for each in crucial:
    for i in each:
        done['D'] = 0
        print(done)
        crucial[i].update(done)

print(crucial)
+4

Python

crucial .update({'D':'0'})
+3

All Articles