Python Dictionaries vs. Dataclasses
1. Introduction and Motivation (10 minutes)
A. Overview
- Context:
- Today we’re exploring dictionaries in Python, a fundamental data structure that maps unique keys to values.
- Motivating Example:
- We’ll work with conference session data. Each session has a unique session ID, a title, a speaker, and a list of topics covered.
- Learning Goals:
- Learn how to create and use dictionaries in Python.
- Practice searching and filtering through dictionary values.
- Compare dictionaries with dataclasses (which provide a fixed, type‑checked structure).
B. The Program Directory Model
- Mental Model:
- Think of the program directory as a mapping from names (or keys) to values. In a dictionary, keys (e.g., session IDs) are associated with their corresponding values (e.g., session details).
- Preview:
- We’ll see how this model makes lookups fast and flexible, and we’ll compare this with dataclasses, which have fixed fields.
2. Creating and Using a Dictionary (15 minutes)
A. Creating a Dictionary for Conference Sessions
- Example Scenario:
- Each session is uniquely identified by a session ID.
- Python Code Example:
# Create a dictionary that maps session IDs to session details.
# Each session is represented as a tuple: (title, speaker, topics)
sessions_dict = {
"CS101": ("Introduction to AI", "Dr. Martinez", ["AI", "Machine Learning"]),
"CS102": ("Deep Learning Techniques", "Prof. Nguyen", ["Neural Networks", "Deep Learning"]),
"CS103": ("Quantum Computing Basics", "Dr. Patel", ["Quantum", "Computing"]),
"CS104": ("Cybersecurity Trends", "Ms. Lee", ["Security", "Networking"])
}
print(sessions_dict) - Discussion:
- Explain that keys (e.g.,
"CS101"
) uniquely identify sessions. - Note that the values can be simple tuples or more complex data structures.
- Explain that keys (e.g.,
B. Accessing and Updating Dictionary Values
- Accessing a Session:
# Lookup the session details for "CS102"
session_details = sessions_dict["CS102"]
print("Session CS102 details:", session_details) - Updating a Session:
- Suppose the speaker for session CS103 changes:
sessions_dict["CS103"] = ("Quantum Computing Basics", "Dr. Singh", ["Quantum", "Computing"])
print("Updated CS103:", sessions_dict["CS103"])
- Suppose the speaker for session CS103 changes:
- Interactive Question:
- Ask: “What happens if you try to access a session ID that does not exist? (Answer: A KeyError is raised.)”
3. Searching Through the Values in a Dictionary (15 minutes)
A. Iterating Over a Dictionary
- Looping Over Keys to Find Sessions by Topic:
# Create an empty dictionary to hold sessions covering 'AI'
ai_sessions = {}
# Loop over each key in sessions_dict
for session_id in sessions_dict:
# Get the details for the current session
details = sessions_dict[session_id]
# Check if 'AI' is one of the topics
# (Assume topics are stored in a list in the third element of the tuple)
if "AI" in details[2]:
ai_sessions[session_id] = details
print("Sessions covering AI:", ai_sessions) - Discussion:
- Explain that we iterated over the dictionary keys, checked each session’s topics, and built a new dictionary of sessions that meet our criterion.
- Interactive Exercise:
- Ask: “How would you find all sessions where the speaker’s name starts with 'Dr.'?”
- Encourage students to outline a similar loop that checks
details[1]
.
4. Dictionaries with More Complex Values and Comparing with Dataclasses (10 minutes)
A. Dictionaries with Complex Values
- Example: Conference Sessions with Detailed Records
- Instead of simple tuples, we can store each session as a dictionary:
sessions_dict = {
"CS101": {"title": "Introduction to AI", "speaker": "Dr. Martinez", "topics": ["AI", "Machine Learning"]},
"CS102": {"title": "Deep Learning Techniques", "speaker": "Prof. Nguyen", "topics": ["Neural Networks", "Deep Learning"]},
"CS103": {"title": "Quantum Computing Basics", "speaker": "Dr. Patel", "topics": ["Quantum", "Computing"]},
"CS104": {"title": "Cybersecurity Trends", "speaker": "Ms. Lee", "topics": ["Security", "Networking"]}
}
print(sessions_dict)
- Instead of simple tuples, we can store each session as a dictionary:
- Accessing Complex Data:
# Retrieve the speaker for session CS104
speaker = sessions_dict["CS104"]["speaker"]
print("Speaker for CS104:", speaker)
B. Dictionaries versus Dataclasses
- Dataclasses Recap (Brief):
- You have seen dataclasses before. For instance:
from dataclasses import dataclass
@dataclass
class ConferenceSession:
session_id: str
title: str
speaker: str
topics: list
session1 = ConferenceSession("CS101", "Introduction to AI", "Dr. Martinez", ["AI", "Machine Learning"])
- You have seen dataclasses before. For instance:
- Comparison:
- Dictionaries:
- More flexible: You can add or remove keys dynamically.
- No built‑in type checking; keys are accessed via strings.
- Great for ad‑hoc or variable structures.
- Dataclasses:
- Fixed structure with type hints.
- Easier for error checking and enforcing a consistent schema.
- Less flexible if you need to add optional or variable fields.
- Dictionaries:
- Interactive Discussion:
- Ask: “What are some advantages of using a dataclass over a dictionary for representing conference sessions? And vice versa?”
5. Wrap-Up (10 minutes)
Recap
- Dictionaries:
- Map unique keys (such as session IDs) to values.
- Allow fast lookup and flexible updating.
- Can store simple or complex values (e.g., tuples, nested dictionaries, lists).
- Dataclasses:
- Provide a fixed, type‑checked structure for compound data.
- Offer clarity and safety but are less flexible than dictionaries.
- When to Use Which:
- Use dictionaries when you need dynamic, fast lookups (e.g., finding a session by ID).
- Use dataclasses when you want a clear, consistent record structure with type checking.