A frozen product catalog is printed as a simple two-column report.
What You Need to Understand
frozendict.keys(), .values() and .items() expose the keys, values and key-value pairs for iteration. The mapping itself cannot change its entries, although mutable nested values can still be modified.
When to Use It
A frozen product catalog is printed as a simple two-column report.
Step-by-Step Python Example
catalog = frozendict({"pen": 2, "paper": 5})
print(list(catalog.keys()))
print(list(catalog.values()))
for product, quantity in catalog.items():
print(product, quantity)
How the Example Works
Each method exposes a different view of the same ordered key-value records, and items() supports pair unpacking.
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
stock = frozendict({"a": 2, "b": 3})
print(sum(stock.values()))
Important Behavior and Edge Cases
Do not assume the result is a mutable list: convert to list() if list operations are needed. Changes to mutable nested objects can still be observed.
This example uses a Python feature. It must be run with Python or newer; earlier interpreters may reject the syntax or lack the built-in type.
Try It Yourself
Use sum() on values to calculate an inventory total.
Common Beginner Mistakes
- Do not assume the result is a mutable list: convert to
list()if list operations are needed. Changes to mutable nested objects can still be observed. - Using the wrong collection method for this operation without checking whether it mutates the object, returns a result, or raises on missing data.
Key Takeaways
frozendict.keys(),.values()and.items()expose the keys, values and key-value pairs for iteration.- Each method exposes a different view of the same ordered key-value records, and
items()supports pair unpacking. - Do not assume the result is a mutable list: convert to
list()if list operations are needed.