Merge Dictionaries in Python
In this guide we'll cover how to merge two (or more) dictionaries in Python. There are a few ways to do it, so we'll cover multiple methods.
For each method, we'll assume we want to merge the following dictionaries:
address = {
'street': '123 W Pine St',
'city': 'Centerville',
'state': 'Indiana',
'size_sq_ft': '2310'
}
resident = {
'first_name': 'Leroy',
'last_name': 'Jenkins',
}
The desired result is a single dictionary that combines the two:
result = {
'street': '123 W Pine St',
'city': 'Centerville',
'state': 'Indiana',
'size_sq_ft': '2310',
'first_name': 'Leroy',
'last_name': 'Jenkins',
}