dictionary.get( ) vs dictionary[ ]

dictionary.get allows you to provide a default value if the key is missing:

dictionary.get("bogus", default_value)

returnsdefault_value(whatever you choose it to be), whereas

dictionary["bogus"]

would raise aKeyError.

If omitted,default_valueisNone, such that

dictionary.get("bogus")  # <-- No default specified -- defaults to None

returnsNonejust like

dictionary.get("bogus", None)

would.

Reference: https://stackoverflow.com/questions/11041405/why-dict-getkey-instead-of-dictkey

Last updated