You are currently viewing Append Python Dictionary to Dictionary

How to append a dictionary as an element to another dictionary in Python? Python provides an update() method to append a dict to an existing dict. The dictionary is mutable hence, you can easily append items to a dictionary or append a dictionary to an original dictionary.

Advertisements

A Python dictionary is a collection that is unordered, mutable, and does not allow duplicates for keys. Each element in the dictionary is in the form of key:value pairs. Dictionary elements should be enclosed with {} and key: value pair separated by commas. The dictionaries are indexed by keys.

Using update() and some more methods we can append the new dictionary at the end of the existing dictionary very easily. In this article, I will explain how to append a dictionary to another dictionary using multiple methods of dictionary with examples.

1. Quick Examples of Append Dictionary to Dictionary

If you are in a hurry, below are some quick examples of how to append a dictionary to another dictionary object.


# Quick examples of appending dictionary to dictionary

# Example 1 : Append the dictionary 
# To another dictionary using update()
my_dict1 = {}
my_dict1.update(my_dict2)

# Example 2: Append dictionary to empty dictionary
my_dict2 = {'course':'python','fee':4000}
my_dict1.update(my_dict2)

# Example 3: Append dictionary 
# To another dictionary
# by merging two dictionaries
new_dict = my_dict1 | my_dict2

# Example 4: Using ** operator append dictionary 
# To another dictionary
new_dict = {**my_dict1, ** my_dict2}

# Example 5: Using items() to append dict 
# To another dict
for key, val in dict.items(my_dict2):
        my_dict1[key] = val
print(my_dict1)

2. Append Python Dictionary to Dictionary using update() Method 

Python provides an update() method in dict class that can be used to append a new dictionary at the ending point of the given dictionary. The update() method allows the dictionary as an argument and adds its key-value pairs to the original dictionary. Let’s create two dictionaries and append them using the update() method.

In the below example, the update() method adds the key-value pairs from my_dict2 to my_dict1. If there are overlapping keys, the values from my_dict2 will overwrite the values in my_dict1.


# Create dictionaries
my_dict1 = {'course':'python','fee':4000}
my_dict2 = {'tutor':'Richerd', 'duration':'45 days'}
print("First Dictionary:",my_dict1)
print("Second Dictionary:",my_dict2)

# Append the dictionary 
# To another dictionary using update()
my_dict1.update(my_dict2)
print("Updated dictionary:\n", my_dict1)

Yields below output.

Append Python dictionary to dictionary

3. Append Python Dictionary to Empty Dictionary

We can also append the dictionary to an empty dictionary using the update() method. Let’s create an empty dictionary and append another dictionary to it.

In the below example, the update() method is used to add the key-value pairs from my_dict2 to my_dict1. After the update, my_dict1 contains the elements of new_dict2.


# Create dictionaries
my_dict1 = {}
my_dict2 = {'course':'python','fee':4000}
print("First Dictionary:",my_dict1)
print("Second Dictionary:",my_dict2)

# Append dictionary to empty dictionary
my_dict1.update(my_dict2)
print("New appended dictionary:\n", my_dict1)

Yields below output.

Append Python dictionary to dictionary

4. Append Dictionary to Dictionary using the Merge â€˜|‘ Operator

Use the merge '|' operator to append given two dictionaries and create a new dictionary. Take two dictionaries and append them using the merge ‘|’ operator.


# Append dictionary to another dictionary
# by merging two dictionaries
new_dict = my_dict1 | my_dict2
print("New appended dictionary:\n", new_dict)

Yields below output.


# Output:
New appended dictionary: 
{'course': 'python', 'fee': 4000, 'tutor': 'Richerd', 'duration': '45 days'}

5. Using the ** operator to Append Dict to Dict in Python

Alternatively, We can append a dictionary to a dictionary by merging two dictionaries using the ** operator. It will append them and create a new appended dictionary. For instance, the {**my_dict1, **my_dict2} syntax creates a new dictionary (new_dict) by unpacking the key-value pairs from both my_dict1 and my_dict2. If there are overlapping keys, the values from my_dict2 will overwrite the values from my_dict1.


# Using ** operator append dictionary to another dictionary
new_dict = {**my_dict1, ** my_dict2}
print("New appended dictionary:\n", new_dict)

Yields below output.


# Output:
New appended dictionary:
{'course': 'python', 'fee': 4000, 'tutor': 'Richerd', 'duration': '45 days'}

6. Using dict.items() method to Append Dict to Dict

Using Python dict.items() method we can append the dictionary to another dictionary. When we pass the dictionary(which we want to add to another dictionary) to dict.items() method it will return the tuple key/value pairs and then iterate it using for loop. For each iteration append the key/value pair to another dictionary. Here, is an example.


# Using items() to append dict to another dict
for key, val in dict.items(my_dict2):
        my_dict1[key] = val
print(my_dict1)

# Output:
# {'course': 'python', 'fee': 4000, 'tutor': 'Richerd', &
# 039;duration': '45 days'}

Frequently Asked Questions on Append Python Dictionary to Dictionary

How do I append one dictionary to another in Python?

In Python, you can append one dictionary to another using either the update() method or the ** unpacking operator.

Can I use the + operator to concatenate dictionaries in Python?

You cannot use the + operator to concatenate dictionaries in Python. The + operator is not defined for dictionaries. To combine dictionaries, you should use the update() method or the ** unpacking operator.

What happens if there are overlapping keys in the dictionaries?

If there are overlapping keys, the values from the second dictionary will overwrite the values in the first dictionary when using update() or the ** unpacking operator.

Is there a way to concatenate dictionaries without modifying the original dictionaries?

You can concatenate dictionaries without modifying the original dictionaries by creating a new dictionary and combining the contents of the originals. You can use the ** unpacking operator to achieve this.

How can I append a dictionary to a list of dictionaries?

To append a dictionary to a list of dictionaries in Python, you can use the append() method of the list. For example, the append() method is used to add the dictionary new_dict to the end of the list list_of_dicts. After the operation, list_of_dicts will contain the new dictionary.

Conclusion

In this article, I have explained how to append a dictionary at the end of another using multiple methods of dict in Python with examples. And also explained using some of the operators how to append/merge the dict to the existing dictionary.