Sitemap

Python dictionaries

3 min readFeb 15, 2026

--

Imagine you’re juggling a phone book or a class’s student records. Names, numbers, grades — everything needs quick access without flipping through pages. Enter Python dictionaries: a powerhouse data structure that stores information in key-value pairs, just like a real-world directory. They’re mutable (changeable), unordered (no fixed sequence), and lightning-fast for lookups. Perfect for beginners tackling real projects like apps or data analysis.

Dictionaries beat lists when you need to grab data by a unique “label” (key) rather than position. Let’s demystify them step by step, with syntax, examples, and code you can copy-paste.

What Are Python Dictionaries?

Think of a dictionary as a phone book. The person’s name (key) points to their number (value). No name? No lookup. Keys must be unique and immutable (like strings or numbers); values can be anything — strings, lists, even other dictionaries.

Why Dictionaries Beat Lists (Phone Book Test)

Lists store items by position. To find something, you often search through them.
Dictionaries work like directories:

  • You provide a key
  • Python returns the value

Example: Phone Book

# Creating an empty dictionary
phone_book = {}

# Creating a dictionary with data
phone_book = {
"Alice": "01234 567890",
"Bob": "09876 543210"
}

# Lookup
print(phone_book["Alice"])

# output
# 01234 567890

Unlike lists, dictionaries provide constant-time lookup (O(1)).
This means retrieval time remains nearly the same whether you store 10 entries or 10 million.

Creating and Modifying Dictionaries

Dictionaries are dynamic. You can add, update, and delete entries easily.
Example: Student Records Dashboard

student_records = {
"Alice": "A",
"Bob": "B"
}

Add or Update

student_records["Charlie"] = "C"   # Adds new key
student_records["Bob"] = "A" # Updates existing key

Delete

del student_records["Charlie"]

# Or use pop() if you need the removed value
grade = student_records.pop("Bob")

After modifications

print(student_records)

# output
# {'Alice': 'A'}

Safe Lookups: No More Crashes

Key missing? Lists error. Dictionaries say “chill.”
Check existence:

"Alice" in student_records  # True/False

Loop Smart: Print Reports Fast

Phone book scan? Student summary?

Keys Only

for name in phone_book:
print(name)

Pairs (gold standard):

for name, number in phone_book.items():
print(f"{name}: {number}")

Nest Deep: Full Profiles

One level? Boring. Nest for power.

students = {
"Alice": {"grade": "A", "phone": "01234 567890"},
"Bob": {"grade": "B", "phone": "09876 543210"}
}
print(students["Alice"]["grade"])

# output
# "A"

Nested dictionaries are commonly used in:

  • Web APIs
  • Configuration files
  • Machine learning feature sets
  • Database-like structures

Essential Dictionary Methods

Here are the core methods every Python developer should know:

Get Suddavanda varshith’s stories in your inbox

Join Medium for free to get updates from this writer.

Get All Keys

keys = student_records.keys()
print(list(keys))

Get All Values

values = student_records.values()

Get All Key–Value Pairs

items = student_records.items()

Copy a Dictionary

backup = student_records.copy()

Clear All Entries

student_records.clear()

Merge Another Dictionary

extras = {"David": "B"}
student_records.update(extras)

Set Default Value3

grade = student_records.setdefault("Eve", "N/A")

Dictionary vs List: A Quick Comparison

Using Lists

students = ["Alice", "Bob", "Charlie"]
grades = ["A", "B", "C"]

index = students.index("Alice")
print(grades[index])

This requires searching for the index.

Using a Dictionary

grades = {"Alice": "A", "Bob": "B"}
print(grades["Alice"])

This directly retrieves the value.
For lookup-heavy tasks, dictionaries are significantly more efficient.

Important Rules and Tips

  • Keys must be immutable (strings, integers, tuples).
  • Lists cannot be dictionary keys.
  • Duplicate keys overwrite previous values.
  • Dictionaries preserve insertion order.

Final Thoughts

Python dictionaries are one of the most powerful and practical data structures in the language.

They allow you to:

  • Retrieve data instantly
  • Organize structured information
  • Build scalable applications
  • Work efficiently with APIs and JSON data

Start simple, experiment more, and soon dictionaries will become one of your favorite tools in Python.

Happy coding!

--

--