Learning overview
- Estimated time
- 4 minutes
- Difficulty
- Beginner
- Prerequisites
- No prior experience required
- Learning outcome
- Explain python using a clear mental model · Apply python in practical Python work
- Last updated
- July 18, 2026
Choose strict or optional lookup
Square brackets express that a key is required and raise KeyError when absent. get expresses optional lookup and can supply a default. Membership with in distinguishes a missing key from a stored falsy value.
course = {"title": "Python", "lessons": 28, "published": False}
title = course["title"]
level = course.get("level", "beginner")
if "published" in course:
print(course["published"])Iterate and transform mappings
Iteration yields keys. Use items for key-value pairs and values for values. Dictionary comprehensions build indexes and transformed mappings clearly.
courses = [
{"slug": "python", "title": "Python"},
{"slug": "sql", "title": "SQL"},
]
by_slug = {course["slug"]: course for course in courses}
for slug, course in by_slug.items():
print(slug, course["title"])Use dedicated tools for counting and grouping
collections.Counter handles counts, while defaultdict can group values without repeated key checks. Use setdefault sparingly when a dedicated collection communicates intent better.
Frequently asked questions
What can be a Python dictionary key?
A key must be hashable with a stable hash and equality behavior, such as strings, numbers, and tuples containing only hashable values.
Do Python dictionaries preserve order?
Yes. Modern Python language semantics preserve insertion order. Updating an existing key does not move its original position.
Topic graph
A taxonomy-generated path through this subject.
Related courses
Related learning
Recommended automatically from shared technologies, topics, intent, and difficulty.
Related Guides
Related Tutorials
Related Glossary
Related Cheatsheets
Related Projects
Related Interview Questions
Related reading
Hand-picked companion pages that deepen this topic.
Your next steps
