How to Add new keys to a Dictionary in Python?

In this tutorial, we will discuss different ways to add new keys to a dictionary in python.

Table Of Contents

Add new keys to a dictionary using []

We can directly use the subscript operator to either change the value of an existing key or to add a new key-value pair into the dictionary.

To add a new key in the dictionary, pass the key in the subscript operator of the dictionary, and assign a value to it. If they given key already exists in the dictionary, then it will update its value, otherwise it will add the new key to the dictionary with the given value.

Let’s understand by some examples,

Advertisements

Add Keys to Dictionary with None as Value

Let’s create a dictionary with three key values pairs, and then add two new keys into it, with None as values.

# Create a dictionary with three key-value pairs
employees={ 1: "Ritika",
            2: "Mark",
            3: "John"}

print("Original Dictionary: ")
print(employees)

# Add 2 keys
employees[4]=None
employees[5]=None

print("Updated Dictionary: ")
print(employees)

Output:

Original Dictionary: 
{1: 'Ritika', 2: 'Mark', 3: 'John'}
Updated Dictionary: 
{1: 'Ritika', 2: 'Mark', 3: 'John', 4: None, 5: None}

We can see that the None value is assigned to key 4 and 5.

Add Keys to Dictionary with proper values

Let’s add two new keys into a dictionary with proper values.

# Create a dictionary with three key-value pairs
employees={ 1: "Ritika",
            2: "Mark",
            3: "John"}

print("Original Dictionary: ")
print(employees)

# Add 2 keys
employees[4] = "David"
employees[5] = "Smriti"

print("Updated Dictionary: ")
print(employees)

Output:

Original Dictionary: 
{1: 'Ritika', 2: 'Mark', 3: 'John'}
Updated Dictionary: 
{1: 'Ritika', 2: 'Mark', 3: 'John', 4: 'David', 5: 'Smriti'}

It added two new keys to the dictionary.

Add new keys to a Dictionary using the update() method

In Python, the dictionary class provides a function update(). It accepts an iterable sequence of key-value pairs as argument and adds them to the dictionary. If any key already exists in the dictionary, then it will update its value. Otherwise it will add all the key-value pairs to the dictionary. Let’s see an example, where we will add two new keys into a dictionary with proper values.

# Create a dictionary with three key-value pairs
employees={ 1: "Ritika",
            2: "Mark",
            3: "John"}

print("Original Dictionary: ")
print(employees)

# Add 2 new key value pairs to a dictionary
employees.update({4 : "David",
                  5 : "Smriti"})

print("Updated Dictionary: ")
print(employees)

Output:

Original Dictionary: 
{1: 'Ritika', 2: 'Mark', 3: 'John'}
Updated Dictionary: 
{1: 'Ritika', 2: 'Mark', 3: 'John', 4: 'David', 5: 'Smriti'}

Add new keys to a Dictionary using setitem () method

The setitem () function of dict class, can be used to add the new key-value to a dictionary. At a time we can add only a single key-value pair. Let’s add 2 key-value pairs to the existing dictionary.

# Create a dictionary with three key-value pairs
employees={ 1: "Ritika",
            2: "Mark",
            3: "John"}

print("Original Dictionary: ")
print(employees)

# Add 2 new key value pairs to a dictionary
employees.__setitem__(4, "David")
employees.__setitem__(5, "Smriti")

print("Updated Dictionary: ")
print(employees)

Output:

Original Dictionary: 
{1: 'Ritika', 2: 'Mark', 3: 'John'}
Updated Dictionary: 
{1: 'Ritika', 2: 'Mark', 3: 'John', 4: 'David', 5: 'Smriti'}

Here, we added two keys to the dictionary.

Add new keys to a Dictionary using ** operator

The operator can be used to add multiple key-value pairs at a time to an existing dictionary. When we apply to a dictionary, it expands the dictionary object to a sequence of key value pairs. Let’s use this to add 2 key-value pairs to the existing dictionary.

# Create a dictionary with three key-value pairs
employees={ 1: "Ritika",
            2: "Mark",
            3: "John"}

print("Original Dictionary: ")
print(employees)

# Add 2 new key value pairs to a dictionary
employees = {**employees, **{4 : "David", 5 : "Smriti"} }

print("Updated Dictionary: ")
print(employees)

Output:

Original Dictionary: 
{1: 'Ritika', 2: 'Mark', 3: 'John'}
Updated Dictionary: 
{1: 'Ritika', 2: 'Mark', 3: 'John', 4: 'David', 5: 'Smriti'}

Here, we added two keys to the dictionary.

Add new keys to a Dictionary using the merge operator

The merge operator (|) is available in python 3.9+ versions. It can join the key-value pairs from two dictionaries. If we want to add the key-value pairs, then put them into a new dictionary and then use this merge operator, such that the key-value pairs in both the dictionaries are joined.

Let’s merge 2 key-value pairs from dictionary2 with the existing dictionary1 and store the merged pairs in dictionary1.

# Consider a dictionary with 3 key-value pairs
dictionary1={1:"Welcome",2:"to",3:"thisPointer"}

print("dictionary1: ",dictionary1)

# Consider a dictionary with 2 key-value pairs
dictionary2={4: 'Python', 5: 'Programming'}

print("dictionary2: ",dictionary2)

# Merge 2 dictionaries into dictionary1
dictionary1= dictionary1 | dictionary2

print("Final: ",dictionary1)

Output:

dictionary1:  {1: 'Welcome', 2: 'to', 3: 'thisPointer'}
dictionary2:  {4: 'Python', 5: 'Programming'}
Final:  {1: 'Welcome', 2: 'to', 3: 'thisPointer', 4: 'Python', 5: 'Programming'}

We can see that {4: ‘Python’, 5: ‘Programming’} are added to dictionary1.

Summary

In this article, we learned about five different methods to add new keys to an existing dictionary in python. Make sure that the merge(|) operator works only on Python 3.9+ version environments.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top