Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Python Add to Dictionary

Python Dictionary basically contains elements in the form of key-value pairs.

It is an unordered collection of items.

Creation of Dictionary:

cities = {"Pune": "Maharashtra", "Ahemdabad": "Gujarat"}
print(cities)
#type(cities)

Output:

{'Pune': 'Maharashtra', 'Ahemdabad': 'Gujarat'}

How to Add to Dictionary in Python

  • By using update() method
  • By using _setitem_() method
  • By using subscript notation
  • By using “*” operator

1. By Using update() Method

The update() method enables the user to add multiple key-value pairs to the dict.

info = {'name':'Safa', 'age':21} 
print("Current Dict is: ", info) 


info.update({'Address':'Pune'}) 
print("Updated Information is: ", info) 

Output:

Current Dict is:  {'name': 'Safa', 'age': 21}
Updated Information is:  {'name': 'Safa', 'age': 21, 'Address': 'Pune'}

2. By Using _setitem_() Method

Python Dictionary’s _setitem_() method is used to add a key-value pair to the dict.

info = {'name':'Safa', 'age':'21'} 
  

info.__setitem__('Address', 'Pune') 
print(info) 

Output:

{'age': '21', 'name': 'Safa', 'Address': 'Pune'}

3. By Using Subscript Notation

The subscript notation helps add a new key-value pair to the dict. If the key doesn’t exist, a new key is created with the mentioned value assigned to it.

Syntax:

dict[new-key]=[new-value]
info = {'name':'Safa', 'age':'21'} 
  
info['Address'] = 'Pune'
 
print(info) 

Output:

{'name': 'Safa', 'age': '21', 'Address': 'Pune'}

4. By Using ” ** ” Operator

The ” ** ” operator basically adds key-value pairs to the new dict and merges it with the old dict.

info = {'name':'Safa', 'age':'21'} #old dict

#adding item to the new dict(result) and merging with old dict(info)  
result = {**info, **{'Address': 'Pune'}}  

print(result) 

Output:

{'name': 'Safa', 'age': '21', 'Address': 'Pune'}

Adding Keys to Nested Python Dictionary

info = {'TEST' : {'name' : 'Safa', 'age' : 21}} 
  

print("The Input dictionary: " + str(info)) 
  

info['TEST']['Address'] = 'Pune'
  

print("Dictionary after adding key to nested dict: " + str(info)) 

Output:

The Input dictionary: {'TEST': {'name': 'Safa', 'age': 21}}
Dictionary after adding key to nested dict: {'TEST': {'name': 'Safa', 'age': 21, 'Address': 'Pune'}}

Addition of Multiple Key-Value Pairs to Python Dictionary

info = {'TEST' : {'name' : 'Safa', 'age' : 21}} 
  

info.update([ ('Address', 'Pune') , ('zip_code',411027 )])
print(info)

Output:

{'TEST': {'name': 'Safa', 'age': 21}, 'Address': 'Pune', 'zip_code': 411027}

Addition of a Dictionary to Another Dictionary

info = {'TEST' : {'name' : 'Safa', 'age' : 21}} 
  
info1 = { 'SET' : {'number' : 452756345, 'Address' : 'Pune'}}
#Adding elements of info1 to info
info.update(info1)
print(info)          

Output:

{'TEST': {'name': 'Safa', 'age': 21}, 'SET': {'number': 452756345, 'Address': 'Pune'}}

Conclusion

Thus, in this article, we have understood and implemented the possible ways to add key-value pairs to Python Dictionary.


References