Just like list comprehensions, Python supports dictionary…

Just like list comprehensions, Python supports dictionary comprehensions.

squares = {x: x * x for x in range(1, 6)}
# {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

even_squares = {
    x: x * x
    for x in range(1, 6)
    if x % 2 == 0
}
# {2: 4, 4: 16}


🔹 13. Dictionary Keys Must Be Hashable

Dictionary keys must be hashable.

Valid examples:
data = {
    "name": "Alex",
    10: "ten",
    (1, 2): "tuple"
}


❌ A list cannot be used as a key.

data = {
    [1, 2]: "numbers"
} # TypeError

Because lists are mutable and therefore unhashable.

🔹 14. Can Dictionary Values Be Mutable?

Yes! Values can be almost any Python object.
data = {
    "numbers": [1, 2, 3],
    "info": {"age": 21}
}

So:
Keys → must be hashable
Values → can be mutable or immutable

🤖 Dictionaries in AI & Data Science

Dictionaries are everywhere in AI applications.

API responses
response = {
    "status": "success",
    "score": 0.94,
    "model": "classifier"
}
Configuration
config = {
    "learning_rate": 0.001,
    "epochs": 10,
    "batch_size": 32
}
Feature mapping
features = {
    "age": 25,
    "income": 60000,
    "experience": 3
}

JSON data - JSON objects map naturally to Python dictionaries.

This makes dictionaries particularly important when working with REST APIs, machine learning pipelines, LLM APIs, and configuration files.

⚠️ Common Beginner Mistakes

❌ Mistake 1: Using an index
student[0]

- Dictionaries are accessed using keys, not numerical indexes.

❌ Mistake 2: Assuming duplicate keys are preserved
data = {
    "name": "Alex",
    "name": "Sam"
} # {'name': 'Sam'}

The final value wins.

❌ Mistake 3: Confusing keys and values
"name" in student

checks whether "name" is a key.

💡 Key Takeaways

✅ Dictionaries store key-value pairs
✅ Keys must be unique
✅ Keys must be hashable
✅ Values can be of different data types
✅ Use [] for direct access
✅ Use .get() when a missing key should be handled safely
✅ .keys(), .values(), and .items() are important methods
✅ Dictionaries support nested structures and comprehensions
✅ They are heavily used in APIs, JSON, AI applications, and data processing

🔥 Mastering dictionaries is essential because you'll use them constantly when working with Python, APIs, data science, machine learning, and AI systems.

Double Tap ❤️ For More
❤5👍1
November 12, 2026 1.4K 4