How to Extend a Dictionary in Python

Problem Formulation and Solution Overview

In this article, you’ll learn how to extend a Dictionary in Python.

To make it more fun, we have the following running scenario:

Weddings-911, an Event Planner start-up, has just booked three (3) new clients and is hoping for many more! This data is saved to the Dictionary weddings.

Beth, the Sales Director, has attained two (2) additional customers and has added them to her own Dictionary, sales_beth.

πŸ’¬ Question: How would we write code to extend the weddings Dictionary to include Beth’s events?

We can accomplish this task by one of the following options:


Method 1: Use update()

This option uses Python’s built-in dict.update() method merging two (2) dictionaries by adding the contents of one (1) dictionary to the end of another. This method removes duplicates.

weddings  = {'Smith-Hamilton' : '01/07/2023',
             'Evans-McGuire'  : '02/14/2023',
             'Klein-Davis'    : '02/19/2023'}

sales_beth = {'Howard-Simms'  : '08/24/2023',
              'Cameron-Bau'   : '09/30/2023'}

weddings.update(sales_beth)
print(weddings)

The above declares two (2) dictionaries: weddings and sales_beth. To add the contents of sales_beth to the end of weddings, call the weddings.update() function and pass sales_beth as an argument.

If successful, weddings should now contain five (5) unique records as this method automatically removes duplicates. In this case, no duplicates were found.

Output

{'Smith-Hamilton': '01/07/2023',
'Evans-McGuire': '02/14/2023',
'Klein-Davis': '02/19/2023',
'Howard-Simms': '08/24/2023',
'Cameron-Bau': '09/30/2023'}

Method 2: Use Union Operator

Another option is to use Python’s built-in Union operator (|), merging two (2) dictionaries by adding the contents of one (1) dictionary to the end of another. This method also removes duplicates.

weddings   = {'Smith-Hamilton' : '01/07/2023',
              'Evans-McGuire'  : '02/14/2023',
              'Howard-Simms'   : '08/24/2023',
              'Klein-Davis'    : '02/19/2023'}

sales_beth = {'Howard-Simms'  : '08/24/2023',
              'Cameron-Bau'   : '09/30/2023'}

new_wdgs = weddings | sales_beth
print(new_wdgs)

The above declares two (2) dictionaries: weddings and sales_beth. For this example, we added a duplicate entry in weddings (Howard-Simms).

The highlighted line assigns the Union of weddings and sales_beth by applying the Union (|) operator, removing duplicates and saving the results to new_wdgs.

If successful, new_wdgs should contain five (5) unique records.

Output

{'Smith-Hamilton': '01/07/2023',
'Evans-McGuire': '02/14/2023',
'Klein-Davis': '02/19/2023',
'Howard-Simms': '08/24/2023',
'Cameron-Bau': '09/30/2023'}

πŸ’‘Note: This method removes the duplicate found in weddings.


Method 3: Use dict() with **kwargs

Another option is to use the dict() function and pass the two (2) dictionaries as arguments and merge.

weddings   = {'Smith-Hamilton' : '01/07/2023',
              'Evans-McGuire'  : '02/14/2023',
              'Howard-Simms'   : '08/24/2023',
              'Klein-Davis'    : '02/19/2023'}

sales_beth = {'Howard-Simms'  : '08/24/2023',
              'Cameron-Bau'   : '09/30/2023'}

new_wdgs = dict(weddings, **sales_beth)
print(new_wdgs)

The above declares two (2) dictionaries: weddings and sales_beth. For this example, there is a duplicate entry in weddings (Howard-Simms).

On the highlighted line, we pass two (2) arguments to dict(): weddings and **sales_beth. The asterisks indicate to dict(): expect variable numbers of keyword arguments. The results save to new_wdgs.

If successful, new_wdgs should contain five (5) unique records as this method automatically removes duplicates.

Output

{'Smith-Hamilton': '01/07/2023',
'Evans-McGuire': '02/14/2023',
'Klein-Davis': '02/19/2023',
'Howard-Simms': '08/24/2023',
'Cameron-Bau': '09/30/2023'}

πŸ’‘Note: This method removes the duplicate found in weddings.


Method 4: Use ChainMap

Python has a built-in ChainMap Class that merges dictionaries. This option requires an additional module, i.e., collections.ChainMap.

from collections import ChainMap

weddings   = {'Smith-Hamilton' : '01/07/2023',
              'Evans-McGuire'  : '02/14/2023',
              'Howard-Simms'   : '08/24/2023',
              'Klein-Davis'    : '02/19/2023'}

sales_beth = {'Howard-Simms'  : '08/24/2023',
              'Cameron-Bau'   : '09/30/2023'}

new_wdgs = ChainMap(weddings, sales_beth)
print(new_wdgs)

First, we call in the Collections library and import ChainMap().

Next, the above declares two (2) dictionaries: weddings and sales_beth. For this example, there is a duplicate entry in weddings (Howard-Simms).

On the last highlighted line, two (2) arguments are passed to ChainMap(): weddings and sales_beth.

If successful, new_wdgs should contain five (6) records as this method does not automatically remove duplicates.

Output

ChainMap({'Smith-Hamilton': '01/07/2023',
'Evans-McGuire': '02/14/2023',
'Howard-Simms': '08/24/2023',
'Klein-Davis': '02/19/2023'},
{'Howard-Simms': '08/24/2023',
'Cameron-Bau': '09/30/2023'})

Method 5: Use Dictionary Comprehension

A simplistic approach to merging dictionaries is Dictionary Comprehension, which uses the **kwargs option for both arguments.

weddings   = {'Smith-Hamilton' : '01/07/2023',
              'Evans-McGuire'  : '02/14/2023',
              'Howard-Simms'   : '08/24/2023',
              'Klein-Davis'    : '02/19/2023'}

sales_beth = {'Howard-Simms'  : '08/24/2023',
              'Cameron-Bau'   : '09/30/2023'}

new_wdgs = {**weddings, **sales_beth}
print(new_wdgs)

The above declares two (2) dictionaries: weddings and sales_beth. For this example, we added a duplicate entry in weddings (Howard-Simms).

On the highlighted line, we pass two (2) arguments to dict(): **weddings and **sales_beth. The asterisks indicate to dict(): expect a variable numbers of keyword arguments.

If successful, new_wdgs should contain five (5) unique records as this method automatically removes duplicates.

Output

{'Smith-Hamilton': '01/07/2023',
'Evans-McGuire': '02/14/2023',
'Klein-Davis': '02/19/2023',
'Howard-Simms': '08/24/2023',
'Cameron-Bau': '09/30/2023'}

πŸ’‘Note: This method removes the duplicate found in weddings.


Summary

These five (5) methods of extending a Dictionary should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!


Programmer Humor

There are only 10 kinds of people in this world: those who know binary and those who don’t.
πŸ‘©πŸ§”β€β™‚οΈ
~~~

There are 10 types of people in the world. Those who understand trinary, those who don’t, and those who mistake it for binary.

πŸ‘©πŸ§”β€β™‚οΈπŸ‘±β€β™€οΈ