If you have two dictionaries that need to be merged, this can be done in two simple ways. You can use the merge operator (
|) or the operator (**). Below we have two dictionaries: first_dict and second_dict. We will use these two methods to merge the dictionaries. Here's the code:1️⃣ Using the merge operator (
|)first_dict = {"kelly": 23,
"Derick": 14, "John": 7}
second_dict = {"Ravi": 45, "Mpho": 67}
combined_dict = first_dict | second_dict
print(combined_dict)Output:
{'kelly': 23, 'Derick': 14, 'John': 7, 'Ravi': 45, 'Mpho': 67}2️⃣ Method 2: using the merge operator (
**)With this operator, you need to put the dictionaries inside curly braces. In the code below, we "substitute" two dictionaries for merging using two operators
*. Both dictionaries are enclosed in curly braces and separated by a comma.first_dict = {"kelly": 23,
"Derick": 14, "John": 7}
second_dict = {"Ravi": 45, "Mpho": 67}
combined_dict = {**first_dict, **second_dict}
print(combined_dict)Output:
{'kelly': 23, 'Derick': 14, 'John': 7, 'Ravi': 45, 'Mpho': 67>👉 https://t.me/DataScience4