Last Updated: January 3, 2026
31 quizzes
Access the price of the 'banana' item from the inventory dictionary and store it in total_cost
total_cost = inventory["banana"][]Click an option to fill the blank:
Safely get a user's preferred_language from the settings dictionary, falling back to 'en' when the key is missing
preferred = settings.get("preferred_language", )Click an option to fill the blank:
Create a dictionary that maps each product code to its length using a dictionary comprehension
code_lengths = {code: for code in product_codes}Click an option to fill the blank:
Which statement correctly creates an empty dictionary in Python?
Given user = {'name': 'Sam', 'age': 25}, which expression safely returns 25?
Which method removes and returns an arbitrary key-value pair from a dictionary?
What does orders.keys() return for a dictionary orders?
Which dictionary comprehension creates {'a': 1, 'b': 2, 'c': 3} from letters = ['a','b','c']?
How do you access the published year of 'book2' in a nested dict library?
Which import is required to use defaultdict?
Which factory is best for grouping words by first letter with defaultdict?
Which OrderedDict method moves a key to the end or beginning?
Given c = Counter(['a','b','a']), what is c['c']?
Which Counter method returns the n most common elements and counts?
Order the steps to build a word frequency dictionary from a list of words using a standard dict.
Drag and drop to reorder, or use the arrows.
Order the steps to create a defaultdict that groups product IDs by category.
Drag and drop to reorder, or use the arrows.
What is the output of this code using a basic dictionary and get()?
1config = {"timeout": 30, "retries": 3}
2value = config.get("delay", config["timeout"])
3print(value)Predict the output when using a dictionary comprehension to filter ages.
1ages = {"Ana": 19, "Bob": 17, "Cara": 21}
2adults = {name: age for name, age in ages.items() if age >= 18}
3print(len(adults))What does this nested dictionary access print?
1catalog = {
2 "books": {"count": 120},
3 "movies": {"count": 45}
4}
5item = "movies"
6print(catalog[item]["count"])What is printed when using defaultdict with int as the factory?
1from collections import defaultdict
2scores = defaultdict(int)
3scores["alice"] += 5
4print(scores["bob"])What is the output when using Counter on a list of tags?
1from collections import Counter
2tags = ['python', 'data', 'python', 'web']
3counts = Counter(tags)
4print(counts['python'])Find the bug in this code that updates a nested dictionary of products by category.
Click on the line(s) that contain the bug.
def add_product(catalog, category, product_name): if category not in catalog: catalog[category] = [] catalog[category][product_name] = True return catalogFind the bug in this code that uses defaultdict to group user IDs by role.
Click on the line(s) that contain the bug.
from collections import defaultdict def group_users_by_role(users): groups = defaultdict() for user in users: role = user['role'] groups[role].append(user['id']) return groupsMatch each dictionary-related concept to its best description.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match each Counter method or behavior with what it does.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Complete the code to create a nested dictionary of users where each user has an email and an 'active' flag set to True.
def create_user_record(username, email): profile = profile[username] = return profileClick an option to fill blank 1:
Complete the code to build a dictionary of squared values for only even numbers from a list.
def even_squares(numbers): return {value: for value in numbers if }Click an option to fill blank 1:
Complete the code to initialize a defaultdict used for aggregating log messages by level.
from collections import defaultdictdef build_log_index(entries): logs_by_level = defaultdict() for level, message in entries: logs_by_level[level].(message) return logs_by_levelClick an option to fill blank 1:
Complete the code to create an OrderedDict sorted by keys from a regular dictionary.
from collections import OrderedDictdef sort_config(config): sorted_items = sorted(config.()) return OrderedDict()Click an option to fill blank 1:
Click the line that will raise a KeyError when accessing a nested dictionary of profiles.
Click on the line to select.
profiles = { "alice": {"email": "a@example.com"}, "bob": {"email": "b@example.com"}}print(profiles["carol"]["email"])Click the line that incorrectly reorders an OrderedDict of tasks.
Click on the line to select.
from collections import OrderedDict tasks = OrderedDict()tasks['low'] = []tasks['medium'] = []tasks['high'] = [] tasks.move_to_end('medium', last=False)print(list(tasks.keys()))