Dictionary Methods
items()
Return a dynamic view of all (key, value) pairs in the dictionary.
dict.items()
dict.items() returns a dictionary view object displaying a list of (key, value) tuple pairs.
The view object reflects any subsequent changes made to the dictionary dynamically.
Returns
A dict_items view of key-value pairs.
Try it
Run it and change it
inventory = {"apple": 10, "orange": 5, "banana": 12}
for item, count in inventory.items():
print(f"{item}: {count}")Worth knowing
- Dynamic updates: When keys/values are added or removed from the dictionary, the
items()view reflects those changes immediately. - Unpacking in loops: Perfect for iterating with tuple unpacking:
for k, v in data.items():.