Open In App

Add a key value pair to Dictionary in Python

Last Updated : 11 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

The task of adding a key-value pair to a dictionary in Python involves inserting new pairs or updating existing ones. This operation allows us to expand the dictionary by adding new entries or modify the value of an existing key.

For example, starting with dictionary d = {'key1': 'geeks', 'key2': 'for'}, we add multiple key-value pairs at once: 'key3': 'Geeks', 'key4': 'is', 'key5': 'portal', and 'key6': 'Computer'. After the update, the dictionary becomes {'key1': 'geeks', 'key2': 'for', 'key3': 'Geeks', 'key4': 'is', 'key5': 'portal', 'key6': 'Computer'}.

Using update()

update() is used when we need to add multiple key-value pairs or modify existing ones. We can pass another dictionary or an iterable of key-value pairs to this method and it will add the new pairs or update the existing keys.

Python
d = {'key1': 'geeks', 'key2': 'for'}

d.update({'key3': 'Geeks', 'key4': 'is', 'key5': 'portal', 'key6': 'Computer'})
print(d)

Output
{'key1': 'geeks', 'key2': 'for', 'key3': 'Geeks', 'key4': 'is', 'key5': 'portal', 'key6': 'Computer'}

Explanation: update() adds key-value pairs to d, updating existing keys and adding new ones .

Using square brackets []

This is the simplest way to add or update a key-value pair in a dictionary. We access the dictionary by specifying the key inside square brackets and assign the corresponding value. If the key doesn't exist, it will be added to the dictionary.

Python
d = {'key1': 'geeks', 'key2': 'for'}

d['key3'] = 'Geeks'
d['key4'] = 'is'
d['key5'] = 'portal'
d['key6'] = 'Computer'
print(d)

Output
{'key1': 'geeks', 'key2': 'for', 'key3': 'Geeks', 'key4': 'is', 'key5': 'portal', 'key6': 'Computer'}

Explanation: It adds new key-value pairs to d using direct assignment.

Using dictionary unpacking

dictionary unpacking allows us to merge dictionaries or add new key-value pairs in a concise way. By using the unpacking operator **, we can merge an existing dictionary with new items in a single statement.

Python
d = {'key1': 'geeks', 'key2': 'for'}

d = {**d, 'key3': 'Geeks', 'key4': 'is', 'key5': 'portal', 'key6': 'Computer'}
print(d)

Output
{'key1': 'geeks', 'key2': 'for', 'key3': 'Geeks', 'key4': 'is', 'key5': 'portal', 'key6': 'Computer'}

Explanation: **d unpacks the original dictionary d , and new key-value pairs are added.

Using dict()

dict() allows us to create a new dictionary by combining existing ones with new key-value pairs. This method is useful when we want to create a new dictionary that includes both the original and new items.

Python
d = {'key1': 'geeks', 'key2': 'for'}

d = dict(d, key3='Geeks', key4='is', key5='portal', key6='Computer')
print(d)

Output
{'key1': 'geeks', 'key2': 'for', 'key3': 'Geeks', 'key4': 'is', 'key5': 'portal', 'key6': 'Computer'}

Explanation: dict(d) creates a new dictionary from d and new key-value pairs are added using keyword arguments.


Python program to Add key:value pair to dictionary

Similar Reads