How To Add Items To a Python Dictionary?

May 23, 2023
How to Add Items to a Python Dictionary



As a Built-in data type in Python, a dictionary entails key-value pairs. The dictionary, as a whole, is mutable though its keys are immutable and unique. Even though the language doesn’t offer a built-in add feature, you can manually add items to the Python dictionary in multiple ways.

python CTA2In this article, we’ll explore some ways to add items to the Python dictionary, including the assignment operator, merge operator, and update operator, besides the commonly used update method.

Python Add to Dictionary: The Assignment Operator

The simplest way to add Python dictionary items is by using = or the assignment operator. The method designates a new value to the key or overwrites the current value. The below example exhibits the assignment operator in action:

dict_example = {'a': 1, 'b': 2}

print("original dictionary: ", dict_example)

dict_example['a'] = 100  # existing key, overwrite

dict_example['c'] = 3  # new key, add

dict_example['d'] = 4  # new key, add

print("updated dictionary: ", dict_example)

Outputoriginal dictionary:  {'a': 1, 'b': 2}updated dictionary:  {'a': 100, 'b': 2, 'c': 3, 'd': 4}

In addition, there is also a way to stop the assignment operator from overwriting the key values. The trick is to add an ‘if’ condition to the syntax.

dict_example = {'a': 1, 'b': 2}

 print("original dictionary: ", dict_example) 

dict_example['a'] = 100  # existing key, overwrite

dict_example['c'] = 3  # new key, add
dict_example['d'] = 4  # new key, add  

print("updated dictionary: ", dict_example) 

# add the following if statements

 if 'c' not in dict_example.keys(): 

   dict_example['c'] = 300 

if 'e' not in dict_example.keys():    

dict_example['e'] = 5

 print("conditionally updated dictionary: ", dict_example)
Output
original dictionary:  {'a': 1, 'b': 2}
updated dictionary:  {'a': 100, 'b': 2, 'c': 3, 'd': 4}
conditionally updated dictionary:  {'a': 100, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

In the example above, the value of ‘c’ remained the same even after the new value definition due to the use of the ‘if’ conditional operator.

Python Add to Dictionary: The Update() Method

Another way to append a Python dictionary or update the current key-value pair is through the update() method. This technique lets you rewrite/overwrite key values with newer ones. The below example illustrates how to use the update() to re-create your Python dictionary.

site = {'Website':'Xperti', 'Tutorial':'How To Add Python Dictionary Items'}

print("original dictionary: ", site)

# update the dictionary with the author key-value pair

site.update({'Author':'Jordon Brown'})

print("updated with Author: ", site)

# create a new dictionary

guests = {'Guest1':'Rick Brown', 'Guest2':'Jim Brown'}

# update the original dictionary with the new dictionary

site.update(guests)

print("updated with new dictionary: ", site

Output

original dictionary:  {'Website': 'Xperti', 'Tutorial': 'How To Add Python Dictionary Items'}

updated with Author:  {'Website': 'Xperti', 'Tutorial': 'How To Add Python Dictionary Items', 'Author': 'Jordon Brown'}

updated with new dictionary:  {'Website': 'Xperti', 'Tutorial': 'How To Add Python Dictionary Items', 'Author': 'Jordon Brown', 'Guest1': 'Rick Brown', 'Guest2': 'Jim Brown'}

Python Add to Dictionary: The Merge | Operator

You can also merge two existing Python dictionaries and create a new one using Merge | operator. The example below illustrates how you can use this to create new dictionaries with ease.

site = {'Website':'Xperti', 'Tutorial':'How To Add Python Dictionary Items', 'Author':'Jordon'}

guests = {'Guest1':'Rick Brown', 'Guest2':'Jim Brown'}

new_site = site | guests

print("site: ", site)

print("guests: ", guests)

print("new_site: ", new_site)

Output

site:  {'Website': 'Xperti', 'Tutorial': 'How To Add Python Dictionary Items', 'Author': 'Jordon'}

guests:  {'Guest1': 'Rick Brown', 'Guest2': 'Jim Brown'}

new_site:  {'Website': 'Xperti', 'Tutorial': 'How To Add Python Dictionary Items', 'Author': 'Jordon', 'Guest1': 'Rick Brown', 'Guest2': 'Jim Brown'}

Python Add to Dictionary: The Update |= Operator

In addition to the methods above, you can also update Python dictionary items through the update |= operator. The below example illustrates how to update values in two dictionaries and create a new updated one.

site = {'Website':'Xperti', 'Tutorial':'How To Add Python Dictionary Items', 'Author':'Jordon'}

guests = {'Guest1':'Rick Brown', 'Guest2':'Jim Brown'}

site |= guests

print("site: ", site)

Output

site:  {'Website': 'Xperti', 'Tutorial': 'How To Add Python Dictionary Items', 'Author': 'Jordon', 'Guest1': 'Rick Brown', 'Guest2': 'Jim Brown'}

The Final Word

Unlike tuples and lists, there is no definite way to add, append, or insert an item in a Python dictionary. So, the trick is to define a new index value to replace the pre-existing one to add or replace Python dictionary items at will.

In this article, we explored multiple ways to use Python’s add-to-dictionary feature with examples. Choose the one that you find the easiest and create better Python dictionaries.

Also Read: How To Use Python Dataclassses?

python CTA



author

jordan

Full Stack Java Developer | Writer | Recruiter, bridging the gap between exceptional talent and opportunities, for some of the biggest Fortune 500 companies.


Candidate signup

Create a free profile and find your next great opportunity.

JOIN NOW

Employer signup

Sign up and find a perfect match for your team.

HIRE NOW

How it works

Xperti vets skilled professionals with its unique talent-matching process.

LET’S EXPLORE

Join our community

Connect and engage with technology enthusiasts.

CONNECT WITH US