Module 21: Advanced Practice

Level up your dictionary skills with these challenges!

Dictionary Add/Update

Modify dictionaries by assigning values to keys. If the key exists, it updates; otherwise, it adds a new entry. Task: Update `"status"` to `"inactive"` and add `"priority": 1`.

# Initial Dictionary:\ndata = { "id": "xyz", "status": "active" }
Result will appear here...

Safe Access with `.get()`

Accessing a non-existent key with `[]` causes an error. Use `.get('key', default_value)` to safely retrieve a value or a default if the key is missing. Task: Select the code that safely gets the value of `"color"`, returning `"default"` if it's missing.

# Dictionary:\noptions = { "size": "large", "enabled": True }

Accessing Nested Data

Dictionaries can contain other dictionaries. Access nested values by chaining the keys using `[]`. Task: Select the code that correctly accesses the `"city"` value.

# Nested Dictionary:
user_data = {
  "id": 101,
  "profile": {
    "name": "Alex",
    "address": {
       "street": "123 Main St",
       "city": "Techville"
    }
  }
}

Conditional Iteration

Combine looping with `if` statements to process dictionary items based on criteria. Task: Iterate through `scores` and print the names of students who scored 90 or higher.

# Student Scores:\nscores = { "Alice": 85, "Bob": 92, "Charlie": 78, "Diana": 95 }
Output will appear here...

Module 21 Advanced Practice Complete!

You've tackled more complex dictionary operations! This is crucial for handling real-world data.
Feel free to review the Basic Practice or watch the Video Lesson again if needed.