Skip to content

Append Python Dictionary to Dictionary Vijetha Spark By {Examples}

  • by

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.

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

Following are 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.

# Create dictionaries
my_dict1 = {‘course’:’python’,’fee’:4000}
my_dict2 = {‘tutor’:’Richerd’, ‘duration’:’45 days’}
print(“Dictionary1:”,my_dict1)
print(“Dictionary2:”,my_dict2)

# Append the dictionary to another dictionary using update()
my_dict1.update(my_dict2)
print(” updated dictionary :n”, my_dict1)

Yields below output.

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.

# Create dictionaries
my_dict1 = {}
my_dict2 = {‘course’:’python’,’fee’:4000}

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

Yields below output.

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.

# 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’, ‘duration’: ’45 days’}

7. 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.

 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. A Python dictionary is a collection that is  Read More Python, Python Tutorial, Python Dictionary Examples 

Leave a Reply

Your email address will not be published. Required fields are marked *