TGViewer
Learn Python Coding Learn Python Coding @pythonre · 40.8K subscribers
Post #9203 1.2K
A bit of Python basics. Day 2: merging dictionaries

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
  • ❤ 4
More from @pythonre
  1. Oct 13, 2026https://substack.com/@husseinsheikho?utm_source=share&utm_medium=android&r=3hwnr8 Follow m…
  2. Oct 10, 2026📚 Download 10 Python books for FREE: 1. Think Python — O’Reilly https://t.co/X7y3pX68IW 2…
  3. Oct 7, 2026Python Lists: A Quick Reference 🐍 * Definition: Lists are ordered, mutable collections th…
  4. Oct 7, 2026📱 270 tools for Python developers The repository contains: 🫡 Linters and formatters; 🫡…
  5. Oct 7, 2026Hands On Python Data Science - Data Science Bootcamp Master Python for Data Science with R…
  6. Oct 6, 2026Post #9851
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →