using get() to return default values from a dictionary

PHOTO EMBED

Thu Sep 16 2021 14:57:14 GMT+0000 (UTC)

Saved by @zeroxenator

user_ids = {
    "John": 12,
    "Anna": 2,
    "Jack": 10
}

# bad practice

name = "Paul"

if name in user_ids:
    user_id = user_ids[name]
else:
    user_id = None

# good practice

user_id = user_ids.get(name, None)
content_copyCOPY