0
0
Pythonprogramming~5 mins

Dictionary keys, values, and items in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AA view object of dictionary keys
BA list of dictionary keys
CA tuple of dictionary keys
DA string of dictionary keys
Which method gives you both keys and values from a dictionary?
Aitems()
Bkeys()
Cvalues()
Dget()
How do you access the value inside a loop using items()?
Afor key in my_dict.items(): value = key
Bfor key, value in my_dict.items(): pass
Cfor value in my_dict.values(): pass
Dfor key in my_dict.keys(): value = key
If you change a dictionary after calling keys(), what happens to the keys view?
AIt stays the same as when created
BIt becomes a list
CIt raises an error
DIt updates to reflect the change
Which of these is NOT true about dictionary views?
AThey reflect changes in the dictionary
BThey can be iterated over
CThey are lists
DThey show current keys, values, or items
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.