Add a key value pair to Dictionary in Python
Last Updated :
11 Jul, 2025
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
Python Dictionary Add Value to Existing Key The task of adding a value to an existing key in a Python dictionary involves modifying the value associated with a key that is already present. Unlike adding new key-value pairs, this operation focuses on updating the value of an existing key, allowing us to increment, concatenate or otherwise adju
2 min read
Add Same Key in Python Dictionary The task of adding the same key in a Python dictionary involves updating the value of an existing key rather than inserting a new key-value pair. Since dictionaries in Python do not allow duplicate keys, adding the same key results in updating the value of that key. For example, consider a dictionar
3 min read
Python - Add Values to Dictionary of List A dictionary of lists allows storing grouped values under specific keys. For example, in a = {'x': [10, 20]}, the key 'x' maps to the list [10, 20]. To add values like 30 to this list, we use efficient methods to update the dictionary dynamically. Letâs look at some commonly used methods to efficien
3 min read
Python Print Dictionary Keys and Values When working with dictionaries, it's essential to be able to print their keys and values for better understanding and debugging. In this article, we'll explore different methods to Print Dictionary Keys and Values.Example: Using print() MethodPythonmy_dict = {'a': 1, 'b': 2, 'c': 3} print("Keys:", l
2 min read
Python - Mapping Key Values to Dictionary We are given two list and we need to map key to values of another list so that it becomes dictionary. For example, we are given two list k = ['a', 'b', 'c'] and v = [1, 2, 3] we need to map the keys of list k to the values of list v so that the resultant output should be {'a': 1, 'b': 2, 'c': 3}.Usi
3 min read
Add Key to Dictionary Python Without Value In Python, dictionaries are a versatile and widely used data structure that allows you to store and organize data in key-value pairs. While adding a key to a dictionary is straightforward when assigning a value, there are times when you may want to add a key without specifying a value initially. In
3 min read