Dictionary and map examples
These examples store player reports by ID, update a report, look up an existing and a missing key, and remove a value.
Python
import json
def print_label_value(label, value):
print(f"\033[1;36m{label}:\033[0m {value}")
def json_value(value):
return json.dumps(value, separators=(",", ":"))
reports_by_id = {
"GS-2042": "Player cannot join a match",
"GS-2043": "Season reward missing",
}
print_label_value("1. Initial reports", json_value(reports_by_id))
reports_by_id["GS-2044"] = "Account access locked"
print_label_value("2. Add report", json_value(reports_by_id))
reports_by_id["GS-2043"] = "Season reward restored"
print_label_value("3. Update report", json_value(reports_by_id))
print_label_value("4. Look up GS-2043", reports_by_id["GS-2043"])
print_label_value("5. Look up GS-2099", reports_by_id.get("GS-2099", "not found"))
removed_report = reports_by_id.pop("GS-2042")
print_label_value("6. Remove GS-2042", removed_report)
print_label_value("7. Remaining reports", json_value(reports_by_id))
JavaScript
function printLabelValue(label, value) {
console.log(`\x1b[1;36m${label}:\x1b[0m`, value);
}
const reportsById = {
"GS-2042": "Player cannot join a match",
"GS-2043": "Season reward missing",
};
printLabelValue("1. Initial reports", JSON.stringify(reportsById));
reportsById["GS-2044"] = "Account access locked";
printLabelValue("2. Add report", JSON.stringify(reportsById));
reportsById["GS-2043"] = "Season reward restored";
printLabelValue("3. Update report", JSON.stringify(reportsById));
printLabelValue("4. Look up GS-2043", reportsById["GS-2043"]);
printLabelValue(
"5. Look up GS-2099",
Object.hasOwn(reportsById, "GS-2099") ? reportsById["GS-2099"] : "not found",
);
const removedReport = reportsById["GS-2042"];
delete reportsById["GS-2042"];
printLabelValue("6. Remove GS-2042", removedReport);
printLabelValue("7. Remaining reports", JSON.stringify(reportsById));
Expected output
1. Initial reports: {"GS-2042":"Player cannot join a match","GS-2043":"Season reward missing"}
2. Add report: {"GS-2042":"Player cannot join a match","GS-2043":"Season reward missing","GS-2044":"Account access locked"}
3. Update report: {"GS-2042":"Player cannot join a match","GS-2043":"Season reward restored","GS-2044":"Account access locked"}
4. Look up GS-2043: Season reward restored
5. Look up GS-2099: not found
6. Remove GS-2042: Player cannot join a match
7. Remaining reports: {"GS-2043":"Season reward restored","GS-2044":"Account access locked"}