5.1.5 Removing Entries with pop()

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):

# Remove and return values using pop()
session_data = {
    "session_id": "sess_98234",
    "user_id": 4012,
    "temporary_passcode": "847291",
    "ip": "192.168.1.10"
}

# 1. Pop and retrieve temporary passcode
extracted_passcode = session_data.pop("temporary_passcode")

# 2. Pop a missing key safely with fallback default
mfa_code = session_data.pop("mfa_secret", "NOT_CONFIGURED")

print("Extracted Passcode:", extracted_passcode)
print("MFA Status:", mfa_code)
print("Remaining session keys:", list(session_data.keys()))
Open in Live Interactive Python Sandbox →