Skip to content
Home / Fundamentals

Python Dictionary Modification

A dictionary in Python is an unordered collection of key-value pairs. It is a mutable data type, which means that we can modify its contents by adding, changing, or removing elements

Add Items

To add a new key-value pair to a dictionary, we can use the assignment operator =. For example:

# Initialize an empty dictionary
my_dict = {}

# Add a key-value pair
my_dict['key'] = 'value'

We can also add multiple key-value pairs at once using the update() method:

# Initialize an empty dictionary
my_dict = {}

# Add multiple key-value pairs
my_dict.update({'key1': 'value1', 'key2': 'value2'})

Change Items

To change the value associated with a particular key in a dictionary, we can simply reassign the value using the assignment operator =. For example:

# Initialize a dictionary with some key-value pairs
my_dict = {'key1': 'value1', 'key2': 'value2'}

# Change the value associated with 'key1'
my_dict['key1'] = 'new value'

Remove Items

There are several ways to remove elements from a dictionary in Python.

Using the del Statement

We can use the del statement to remove a specific key-value pair from a dictionary:

# Initialize a dictionary with some key-value pairs
my_dict = {'key1': 'value1', 'key2': 'value2'}

# Remove the 'key1' key-value pair
del my_dict['key1']

Using the pop() Method

We can use the pop() method to remove a specific key-value pair from a dictionary and return its value:

# Initialize a dictionary with some key-value pairs
my_dict = {'key1': 'value1', 'key2': 'value2'}

# Remove the 'key1' key-value pair and store its value
value = my_dict.pop('key1')

Using the popitem() Method

We can use the popitem() method to remove and return a randomly chosen key-value pair from a dictionary:

# Initialize a dictionary with some key-value pairs
my_dict = {'key1': 'value1', 'key2': 'value2'}

# Remove a random key-value pair and store it as a tuple
key_value_pair = my_dict.popitem()

Combine Dictionaries

There are several ways to combine (or merge) dictionaries in Python.

Using the update() Method

We can use the update() method to add the key-value pairs from one dictionary to another:

# Initialize two dictionaries with some key-value pairs
dict1 = {'key1': 'value1', 'key2': 'value2'}
dict2 = {'key3': 'value3', 'key4': 'value4'}

# Add the key-value pairs from dict2 to dict1
dict1.update(dict2)

# dict1 now contains all of the key-value pairs from both dictionaries
print(dict1)  # {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}

### Using the {**dict1, **dict2} Syntax
We can use the {**dict1, **dict2} syntax to merge two dictionaries into a new dictionary:

```python
# Initialize two dictionaries with some key-value pairs
dict1 = {'key1': 'value1', 'key2': 'value2'}
dict2 = {'key3': 'value3', 'key4': 'value4'}

# Merge the dictionaries into a new dictionary
merged_dict = {**dict1, **dict2}

# merged_dict is a new dictionary that contains all of the key-value pairs from both dictionaries
print(merged_dict)  # {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}

Best Practices

Add Items

When adding multiple key-value pairs to a dictionary at once, consider using the update() method instead of repeatedly using the assignment operator =. This is more efficient and easier to read.

Change Items

When changing the value associated with a particular key in a dictionary, be sure to use the correct key. A KeyError exception will be raised if the key does not exist in the dictionary.

Remove Items

When using the del statement or the pop() method to remove a key-value pair from a dictionary, be sure to use the correct key. A KeyError exception will be raised if the key does not exist in the dictionary.

Consider using the pop() method instead of the del statement if you need to store the value of the removed key-value pair.

Combine Dictionaries

When combining dictionaries, consider the order in which the key-value pairs are added. If a key exists in both dictionaries, the value from the second dictionary will overwrite the value from the first dictionary.

Consider using the update() method or the {**dict1, **dict2} syntax to create a new dictionary that contains all of the key-value pairs from both dictionaries. This is more efficient and easier to read than using a loop to manually add the key-value pairs.