Skip to main content

Extra - Dictionaries

Skills: None

Pre-reading: 9.2

Intro (15 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 (40 mins)

  1. Create a dictionary mapping student IDs to names. Add a new student, update a name, and remove a student.
  2. Given a dictionary of conference sessions (as above), print all session IDs for sessions covering "AI".
  3. Count how many sessions have a speaker whose name starts with "Dr.".
  4. Add a new topic to the list of topics for a given session.
  5. What happens if you try to access a session that doesn't exist? How can you avoid a crash?
  6. Write a function that takes a dictionary of sessions and returns a list of all unique topics covered.
  7. Convert a list of ConferenceSession dataclass instances into a dictionary mapping session IDs to dataclass instances.
  8. Filter all sessions where the number of topics is greater than 1.
  9. Download the CSV file from here and read it in Python however you like.
  10. Create a dictionary that has people's names as keys and a list of movies for each person as the values.
  11. Use a dictionary to track each movie that is listed, and track the number of times a movie appears in the dataset.
  12. Use a dictionary to track each movie and the people that like the particular movie.

Wrap-up (5 mins)

  • Dictionaries map unique keys to values and are flexible for dynamic data.
  • Dataclasses provide a fixed, type-checked structure.