How to add new keys to a dictionary in Python?



In Python, a Dictionary is one of the data structures that contains key-value pairs enclosed in curly braces '{}'. It is a Mutable and unordered data structure.

Before proceeding with adding new keys to a dictionary, first let's create a dictionary with key and values. Following is the example of creating a dictionary with specified key and values -

dict1 = {'key1': 'Tutorials', 'key2': 'Point','key3': 'Python'}
print("Created Dictionary:", dict1)

Following is the output of the above example -

Created Dictionary: {'key1': 'Tutorials', 'key2': 'Point', 'key3': 'Python'}

Adding new keys to a Dictionary

Once a Dictionary is created, if required we can add new keys to it by using different methods such as update(), assignment operator etc., available in Python.

Using Assignment Operator (=)

When we want to add a new key to a dictionary, we have to assign a value to a key by using assignment operator (=) then python will automatically add this new key-value pair to the dictionary. Following is the example of adding a new key to the dictionary by using Assignment operator (=) -

dict1 = {'key1': 'Tutorials', 'key2': 'Point','key3': 'Python'}
print("Original Dictionary:",dict1)
dict1['Brand'] = 'Apple'
print("Updated Dictionary:", dict1)

Here is the output of the above example -

Original Dictionary: {'key1': 'Tutorials', 'key2': 'Point', 'key3': 'Python'}
Updated Dictionary: {'key1': 'Tutorials', 'key2': 'Point', 'key3': 'Python', 'Brand': 'Apple'}

Using update() Method

In Python, the update() method is used to add one or more key-value pairs to an existing dictionary. If the key already exists then the value of the key will be updated. If the key does not exist then key will be added as a new key. This method is used when we want to add multiple entries at a time. Following is the example of adding a new key to the dictionary using the update() method -

dict1 = {'key1': 'Tutorials', 'key2': 'Point', 'key3': 'Python'}
print("Original Dictionary:", dict1)
dict1.update({'Brand': 'Apple'})
print("Updated Dictionary:", dict1)

Below is the output of the above example -

Original Dictionary: {'key1': 'Tutorials', 'key2': 'Point', 'key3': 'Python'}
Updated Dictionary: {'key1': 'Tutorials', 'key2': 'Point', 'key3': 'Python', 'Brand': 'Apple'}

Using "in" operator and "if" statement

In Python, the in operator is used to check whether a specified value exists in a sequence such as a list, tuple, string or dictionary. It returns True if the value is found in the sequence otherwise returns False.

When we combine in with an if statement then it checks for the given condition based on the presence of a value.

fruits = ["apple", "banana", "cherry"]

if "banana" in fruits:
    print("Banana is in the list")
else:
    print("Banana is not in the list")

Here is the output of the above example -

Banana is in the list

Using the Merge | Operator

The | merge operator in Python is used to merge multiple dictionaries into one with a single expression and returns the combined dictionary as a new dictionary. If both dictionaries have the same key, then the value from the dictionary on the right side of the operator will be replaced with the value from the left dictionary.

Following is the example, which merges two different dictionaries into one using the | Operator -

# Two sample dictionaries
employee_info = {
    "name": "Niharikaa",
    "department": "Developer"
}

employee_update = {
    "department": "Programming",
    "location": "New York"
}

# Merging using the | operator in a single expression
merged_dict = employee_info | employee_update

print("Merged Dictionary =", merged_dict)

Following is the output of the above program -

Merged Dictionary = {'name': 'Niharikaa', 'department': 'Programming', 'location': 'New York'}
Updated on: 2025-09-01T14:57:36+05:30

982 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements