5.1.7 Inspecting Keys with keys()

Unit: Unit 5: Dictionaries and Sets • Interactive Practice

Quick Answer & Summary:

Interactive Python programming tutorial and practice in your browser.

Interactive Challenge (Run Live in Browser):

# Inspect dictionary keys using keys() and view objects
metrics = {
    "latency_ms": 24,
    "throughput_rps": 1250,
    "error_rate": 0.002,
    "uptime_pct": 99.98
}

# 1. Retrieve the dict_keys view object
keys_view = metrics.keys()

# 2. Convert keys view into an ordered list
keys_list = list(keys_view)

# 3. Iterate directly over dictionary keys
print("Registered Metric Names:")
for key in metrics.keys():
    print("-", key)

print("Keys as list:", keys_list)
Open in Live Interactive Python Sandbox →