759
Comment: Clarified Wording a bit
|
783
Clarification
|
Deletions are marked like this. | Additions are marked like this. |
Line 3: | Line 3: |
If you don't want to have an exception but default value used instead, you can use `get()` method: | If you don't want to have an exception but would rather a default value used instead, you can use the `get()` method: |
Line 12: | Line 12: |
of key only if it is not already in the dict, and returns the value in any case: | of the key only if it is not already in the dict, and returns that value in any case: |
Python raises a KeyError whenever a dict() object is requested. Using the format a = adict[key] if the key is not in the dictionary.
If you don't want to have an exception but would rather a default value used instead, you can use the get() method:
Even more handy is somewhat controversially-named setdefault(key, val) which sets the value of the key only if it is not already in the dict, and returns that value in any case: