Is there a better way of creating a nested dictionary than what I'm doing below? The result for the setdefault is because I don't know whether that particular key exists yet or not.
def record_execution_log_action(
execution_log, region, service, resource, resource_id, resource_action
):
execution_log["AWS"].setdefault(region, {}).setdefault(service, {}).setdefault(
resource, []
).append(
{
"id": resource_id,
"action": resource_action,
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
}
)
defaultdictfromcollectionsmodule instead, it automatically creates the default( this can be alist) if thekeyis not found. \$\endgroup\$defaultdictbut I don't get how to use it for my nested use case properly. Could you give an example? \$\endgroup\$