How to Add Values to Dictionary in Python
The task of adding values to a dictionary in Python involves inserting new key-value pairs or modifying existing ones. A dictionary stores data in key-value pairs, where each key must be unique. Adding values allows us to expand or update the dictionary's contents, enabling dynamic manipulation of data.
For example, if we start with a dictionary a = {0: 'Carrot', 1: 'Raddish'}, we can add a new key-value pair like a[2] = 'Brinjal', resulting in the dictionary {0: 'Carrot', 1: 'Raddish', 2: 'Brinjal'}.
Using union operator
union operator (|
) is a efficient way to add values by merging dictionaries as it creates a new dictionary with the combined key-value pairs, leaving the original dictionaries unchanged.
a = {0: 'Carrot', 1: 'Raddish'}
b = {2: 'Brinjal', 3: 'Potato'}
res = a | b
print(res)
Output
{0: 'Carrot', 1: 'Raddish', 2: 'Brinjal', 3: 'Potato'}
Explanation: a | b combines all key-value pairs from a and b, with values from b overwriting those in a if duplicate keys exist .
Table of Content
Using unpacking
** unpacking syntax expands dictionaries into key-value pairs ,making it a clean and effective way to merge dictionaries into a new one .
a = {0: 'Carrot', 1: 'Raddish'}
b = {2: 'Brinjal', 3: 'Potato'}
res = {**a, **b}
print(res)
Output
{0: 'Carrot', 1: 'Raddish', 2: 'Brinjal', 3: 'Potato'}
Explanation: {**a, **b} merges dictionaries by unpacking all key-value pairs from a and b, with values from b overwriting those in a if duplicate keys exist.
Using update()
update() method allows us to add multiple key-value pairs from another dictionary or an iterable of key-value pairs to an existing dictionary. If a key exists, its value will be updated.
a = {0: 'Carrot', 1: 'Raddish'}
b = {2: 'Brinjal', 3: 'Potato'}
res = a.copy() # creates a shallow copy of dictionary `a`
res.update(b)
print(res)
Output
{0: 'Carrot', 1: 'Raddish', 2: 'Brinjal', 3: 'Potato'}
Explanation: res.update(b) merges b into res by adding all key-value pairs, updating existing keys in res if duplicates exist, and appending new keys from b .
Using dictionary comprehension
This method iterates over key-value pairs from one or more dictionaries and constructs a new dictionary. While less efficient, it is useful when we need to apply logic while adding values.
a = {0: 'Carrot', 1: 'Raddish'}
b = {2: 'Brinjal', 3: 'Potato'}
res = {k: v for d in (a, b) for k, v in d.items()}
print(res)
Output
{0: 'Carrot', 1: 'Raddish', 2: 'Brinjal', 3: 'Potato'}
Explanation: dictionary comprehension merges a and b by iterating over both dictionaries and collecting key-value pairs into a new dictionary.
Using assignment operator
This is the simplest way to add a single key-value pair to a dictionary. If the key already exists, its value will be updated.
a = {0: 'Carrot', 1: 'Raddish'}
# Adding a single key-value pair
a[2] = 'Brinjal'
print(a)
Output
{0: 'Carrot', 1: 'Raddish', 2: 'Brinjal'}
Explanation: a[2] = 'Brinjal' adds key 2 with the value 'Brinjal' to dictionary a.