Add New Keys to a Dictionary in Python



Adding new keys to a dictionary in Python, can done by simply assigning a value to a key that isn't present. There are several ways to do this, which include different methods and techniques for adding keys and their corresponding values.

  • Using update() Method

  • Using Assignment (=) operator

  • Using the 'in' operator and 'if' statements

  • Using Merge | Operator

  • Using Subscript notation

Using the 'update()' Method

If we want to update or add a lot of keys to the dictionary at once then using the update() method is suitable. This method is useful when you want to insert several elements without assigning each one individually.

Example

In this example, the update() method adds the new key 'key3' with the value 'Python' to the existing dictionary.

dict = {'key1': 'Tutorials', 'key2': 'Point'}
print("This is my current dict:" , dict)
 
# adding new key 
dict.update({'key3': 'Python'})

#Display the updated dictionary
print("This is my updated dict:", dict)

Output

This is my current dict: {'key1': 'Tutorials', 'key2': 'Point'}
This is my updated dict: {'key1': 'Tutorials', 'key2': 'Point', 'key3': 'Python'}

Using Assignment Operator

You can also add a new key-value pair by directly assigning a value to a new key by using the assignment (=) operator. This method is simple and effective for adding single entries.

Example

Here, a new key 'Cherry' is added with the value 30, demonstrating how straightforward it is to expand a dictionary using the assignment operator.

# Defining  existing dictionary
my_dict = {'Apple': 10, 'Banana': 20}

# Adding new key-value pair
my_dict['Cherry'] = 30

# Display the updated dictionary
print(my_dict)

Output

{'Apple': 10, 'Banana': 20, 'Cherry': 30}

Using 'in' Operator With 'if' Statements

If you want to add a key only if it doesn't exist in the dictionary, you can use the 'in' operator combined with an if statement. This method helps to prevent overwriting existing keys.

Example

In this example, the code checks for the existence of the key 'd'. Since it doesn't exist, it adds it with the value 4.

mydict = {"a": 1, "b": 2, "c": 3}
 
if "d" not in mydict:
    mydict["d"] = "4"
else:
    print("Key is already existed in dictionary")
 
print(mydict)

Output

{'a': 1, 'b': 2, 'c': 3, 'd': '4'}

Using the Merge | Operation

The merge operator | allows you to combine dictionaries easily. This method is particularly useful for adding multiple key-value pairs from another dictionary.

Example

Here, the existing dictionary my_dict is updated (merged) to include the new keys and values from new_data.

# Existing dictionary
dict = {'name': 'Robert', 'age': 27}
 
# Adding New key-value pairs 
new_data = {'city': 'Mumbai', 'gender': 'Male'}
 
# Using the merge operator
dict |= new_data
 
# Display the updated dictionary
print(dict)

Output

{'name': 'Robert', 'age': 27, 'city': 'Mumbai', 'gender': 'Male'}

Using the Subscript notation

The Subscript notation technique allows you to add or modify key-value pairs in a dictionary. If the key already exists, its value will be overwritten; if it does not exist, a new key-value pair will be added.

Syntax

Following is the syntax for subscript notation.

Dictionary_Name[New_Key_Name] = New_Key_Value

Example

In this case, the value of the existing key 'key2' is updated to 'Point', and a new key 'key3' is added with the value 'Python'

dict = {'key1': 'Tutorials', 'key2': 'modify'}
print("Current Dict is:", dict)
 
# using subscript notation
dict['key2'] = 'Point'
dict['key3'] = 'Python'
print("Updated Dict is:", dict)

Output

Current Dict is: {'key1': 'Tutorials', 'key2': 'modify'}
Updated Dict is: {'key1': 'Tutorials', 'key2': 'Point', 'key3': 'Python'}
Updated on: 2025-03-05T17:40:33+05:30

900 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements