Update dictionary with another dictionary python

Python dictionary is one of the built-in data types. Dictionary elements are key-value pairs. You can add to dictionary in Python using multiple methods. Let’s take a look at some of the different ways to append items to an existing Python dictionary.

Python add to Dictionary using “=” assignment operator

We do not have any specific Python way to update a dictionary. If you want to add a new key to the dictionary, then you can use the assignment operator with the dictionary key.

This is pretty much the same as assigning a new value to the dictionary. Since Python dictionaries are mutable, when you use the assignment operator, you’re simply adding new keys to the datastructure.

dict[key] = value

When a key already exists in the dictionary, the assignment operator will automatically update the values.

Let’s take a look at a demonstration below to see how this works out. If there is an existing key, the assignment operator will overwrite the values automatically.

d = {'a': 1, 'b': 2}
print(d)
d['a'] = 100  # existing key, so overwrite
d['c'] = 3  # new key, so add
d['d'] = 4
print(d)

Output:

{'a': 1, 'b': 2}
{'a': 100, 'b': 2, 'c': 3, 'd': 4}

Append values to a dictionary using the update() method

The Python dictionary offers an update() method that allows us to append a dictionary to another dictionary. The update() method automatically overwrites the values of any existing keys with the new ones. So make sure that you do not accidentally overwrite any valueable piece of information here.

Let’s see how we can use the dictionary update() method to add new values to our dictionary:

blog = {'Website':'Journaldev', 'tutorial':'Append to Python dictionary'}
print("Here are the current details: ", blog)

# Adding the author details to the dictionary
blog.update({'Author':'Pankaj Kumar'})
print("Updated dictionary is: ", blog)

# Appending another dictionary
guests = {'Guest1':'Meghna'}
blog.update(guests)
print("Updated dictionary is: ", blog)

Let’s take a look at how the update() method works to add new items to a Python dictionary:

Here are the current details:  {'Website': 'Journaldev', 'tutorial': 'Append to Python dictionary'}
Updated dictionary is:  {'Website': 'Journaldev', 'tutorial': 'Append to Python dictionary', 'Author': 'Pankaj Kumar'}
Updated dictionary is:  {'Website': 'Journaldev', 'tutorial': 'Append to Python dictionary', 'Author': 'Pankaj Kumar', 'Guest1': 'Meghna'}

Add items to Python dictionary without overwriting

Notice how both the methods automatically overwrite any existing value if the key is present. If you know that your program may end up having duplicate keys, it’s best to add a conditional append instead of directly appending the values.

You can do the same using an if condition here to ensure that the keys within the dictionary are not overwritten.

Here’s a simple example of how you can make this work. You can also choose to use the try catch exception however, using an if condition will be the easiest one to start with.

if 'c' not in d.keys():
    d['c'] = 300

if 'e' not in d.keys():
    d['e'] = 5

print(d)

Output:

{'a': 100, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

Notice that ‘c’ value is not changed because of the if condition.

Conclusion

That’s all for adding keys to a dictionary in python.

How do I update two dictionaries in Python?

Below are the eight standard methods by which you can merge two dictionaries in python..
1) Using update() method..
2) Using merge(|) operator..
3) Using ** operator..
4) Unpacking the second dictionary..
5) Using collection.ChainMap() method..
6) Using itertools. ... .
7) Using dictionary comprehension..
8) Add values of common keys..

Can dictionary be updated in Python?

Python Dictionary update() Method The update() method inserts the specified items to the dictionary. The specified items can be a dictionary, or an iterable object with key value pairs.

How do you append a dictionary as a value to another dictionary in Python?

To append an element to an existing dictionary, you have to use the dictionary name followed by square brackets with the key name and assign a value to it.

Does dict update overwrite?

The Python dictionary offers an update() method that allows us to append a dictionary to another dictionary. The update() method automatically overwrites the values of any existing keys with the new ones.