python: dict.get()
Use dict.get(key[, default])
to assign default values
The code below is functionally equivalent to the original code above, but this solution is more concise.
When get()
is called, Python checks if the specified key exists in the dict. If it does, then get()
returns the value of that key. If the key does not exist, then get()
returns the value specified in the second argument to get()
.
dictionary = {"message": "Hello, World!"} data = dictionary.get("message", "") print(data) # Hello, World!