Day 33 - Dictionaries
Skills: None
Pre-reading: 9.2
Intro (10 mins)
- Today we explore dictionaries in Python, a data structure that maps unique keys to values.
- Dictionaries are useful for fast lookups and flexible data storage, while dataclasses provide a fixed, type-checked structure.
- Example: Conference session data, where each session has a unique session ID, a title, a speaker, and a list of topics.
# Dictionary mapping session IDs to session details (as dictionaries)
sessions = {
"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["CS101"]["speaker"]) # Dr. Martinez - You can update values, add new keys, or remove keys:
sessions["CS103"]["speaker"] = "Dr. Singh"
sessions["CS105"] = {"title": "Data Ethics", "speaker": "Dr. Kim", "topics": ["Ethics", "Data"]}
del sessions["CS104"] - If you try to access a key that doesn't exist, you'll get a
KeyError
. You can use.get()
to avoid this:print(sessions.get("CS999", "Not found")) # Not found
- You can iterate through a dictionary to search or filter:
# Print all session IDs for sessions covering "AI"
for session_id, details in sessions.items():
if "AI" in details["topics"]:
print(session_id)
# Count how many sessions have a speaker whose name starts with "Dr."
count = 0
for details in sessions.values():
if details["speaker"].startswith("Dr."):
count += 1
print(count) - You can collect all unique topics (using a list and checking for duplicates):
unique_topics = []
for details in sessions.values():
for topic in details["topics"]:
if topic not in unique_topics:
unique_topics.append(topic)
print(unique_topics) - Dictionaries vs. dataclasses:
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"]) - Dictionaries are flexible and dynamic; dataclasses are fixed and type-checked.
Class Exercises (35 mins)
- Create a dictionary mapping student IDs to names. Add a new student, update a name, and remove a student.
- Given a dictionary of conference sessions (as above), print all session IDs for sessions covering "AI".
- Count how many sessions have a speaker whose name starts with "Dr.".
- Add a new topic to the list of topics for a given session.
- What happens if you try to access a session that doesn't exist? How can you avoid a crash?
- Write a function that takes a dictionary of sessions and returns a list of all unique topics covered.
- Convert a list of
ConferenceSession
dataclass instances into a dictionary mapping session IDs to dataclass instances. - Filter all sessions where the number of topics is greater than 1.
Wrap-up (5 mins)
- Dictionaries map unique keys to values and are flexible for dynamic data.
- Dataclasses provide a fixed, type-checked structure.