Recall & Review
beginner
What does the
keys() method of a dictionary return?The
keys() method returns a view object that displays a dynamic view of all the keys in the dictionary.Click to reveal answer
beginner
How can you get all the values from a dictionary?
You can use the
values() method to get a view object containing all the values in the dictionary.Click to reveal answer
beginner
What does the
items() method return in a dictionary?The
items() method returns a view object of tuples, where each tuple contains a key and its corresponding value.Click to reveal answer
beginner
How can you loop through both keys and values of a dictionary at the same time?
Use a for loop with
items(), like: for key, value in my_dict.items(): to access both keys and values.Click to reveal answer
intermediate
Are the objects returned by
keys(), values(), and items() lists?No, they are view objects that reflect changes in the dictionary but behave like sets or sequences, not regular lists.
Click to reveal answer
What type of object does
my_dict.keys() return?✗ Incorrect
The
keys() method returns a view object, not a list or tuple.Which method gives you both keys and values from a dictionary?
✗ Incorrect
The
items() method returns key-value pairs as tuples.How do you access the value inside a loop using
items()?✗ Incorrect
Using
for key, value in my_dict.items(): unpacks each key-value pair.If you change a dictionary after calling
keys(), what happens to the keys view?✗ Incorrect
The keys view reflects changes in the dictionary dynamically.
Which of these is NOT true about dictionary views?
✗ Incorrect
Dictionary views are not lists; they are special view objects.
Explain how to get and use the keys, values, and items of a dictionary in Python.
Think about how you can see just keys, just values, or both together.
You got /4 concepts.
Describe what happens to the objects returned by keys(), values(), and items() if the dictionary changes.
Consider if these objects are snapshots or live views.
You got /3 concepts.