Problem Formulation and Solution Overview
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
.
Method 1: Use update()
This option uses Python’s built-in
method merging two (2) dictionaries by adding the contents of one (1) dictionary to the end of another. This method removes duplicates.dict.update()
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', |
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', |
π‘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', |
π‘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', |
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', |
π‘Note: This method removes the duplicate found in weddings
.
Summary
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.
π©π§ββοΈπ±ββοΈ