Back to Python Fundamentals

Python Dictionaries

Learn how to store key–value data using dictionaries and why they are essential for storing structured information.

8 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.

Ready to continue?

Mark this lesson as complete when you're ready to proceed.

Key moments

  1. Dictionary Basics — Creates a dictionary storing country keys mapped to capital values using curly braces.
  2. Dictionary Introspection — Demonstrates how to inspect available dictionary methods using dir and help functions.
  3. Safe Retrieval with Get — Retrieves values using get and handles non-existent keys conditionally via None return values.
  4. Updating and Inserting — Inserts a new country and modifies an existing capital value using the update method.
  5. Removal Methods — Removes entries using pop for specific keys, popitem for recent keys, and clear for wiping the dictionary.
  6. Keys and Values Iteration — Iterates across keys and values collections using dedicated view methods within for loops.
  7. Iterating Over Items — Unpacks key-value tuple pairs across loop iterations using the items method.
PDF notes

Frequently asked questions

What happens if I define duplicate keys when initializing a dictionary?

Python retains only the last value assigned to that key, overwriting any previous assignments without raising an error.

Why does .get('missing_key') return None instead of an error?

The get method is designed for safe lookups, returning None by default when a key does not exist so your program does not crash.

What is the difference between pop() and popitem()?

The pop method requires a specific key argument to delete, whereas popitem takes no arguments and removes the most recently inserted pair.

How do dir() and help() differ when inspecting dictionaries?

The dir function lists all attribute and method names as strings, while help prints interactive, detailed documentation describing what each method does.

How was this lesson?

Your feedback helps us refine explanations and catch bugs.