In python, there are multiple ways to add keys or values to dictionary.
You can use assignment operator to add key to dictionary.
dic[‘x’]=1
There are other ways to add to dictionay as well in python.
# OR
dic.update(dict(x=1))
# OR
dic.update(x=1)
Example:
1 2 3 4 5 6 |
d = {'x':1,'y':2,'z':3} print("Before:",d) d['p']='24' print("After:",d) |
Output:
After: {‘x’: 1, ‘y’: 2, ‘z’: 3, ‘p’: ’24’}