TGViewer
Python Learning Python Learning @python_bds · 5.74K subscribers
Post #1284 610
🧠 dict.get() in Python

Suppose you have this dictionary.
user = {
"name": "Alice",
"age": 24
}


Now you try to access a key that doesn't exist.
print(user["email"])

🔻Python raises:
KeyError: 'email'


Sometimes that's exactly what you want. A missing key should crash the program.

But often, a missing value is perfectly normal.
Instead of checking manually:
if "email" in user:
email = user["email"]
else:
email = None

🟢 Python provides:
email = user.get("email")


If the key exists, you get its value.
If it doesn't, you get None instead of a crash.

You can even choose a default value.
email = user.get("email", "Not provided")


👉 get()
isn't shorter just for the sake of being shorter. It expresses the idea that a missing key is expected.
  • ❤ 2
  • 👍 2
More from @python_bds
  1. Oct 6, 2026🐍 Python Performance Optimization Python Performance Optimization: Make Your Code Faster…
  2. Oct 4, 2026Python Set Methods ✍️
  3. Oct 3, 2026Post #1310
  4. Oct 1, 2026🐍 Python Beginner Notes
  5. Sep 30, 2026🧠 return vs print() in Python These are not interchangeable. def add(a, b): print(a + b)…
  6. Sep 28, 2026PYTHON SKILL ROADMAP │ ├── 📁 Python Basics │ ├── 📁 Variables & Data Types │ ├── 📁 Input…
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 →