Python Language Tutorial => Dictionary
Syntax
- mydict = {}
- mydict[k] = value
- value = mydict[k]
- value = mydict.get(k)
- value = mydict.get(k, "default_value")
Parameters
| Parameter | Details |
|---|---|
| key | The desired key to lookup |
| value | The value to set or return |
Remarks
Helpful items to remember when creating a dictionary:
- Every key must be unique (otherwise it will be overridden)
- Every key must be hashable (can use the
hashfunction to hash it; otherwiseTypeErrorwill be thrown) - There is no particular order for the keys.
- Introduction to Dictionary
- Accessing keys and values
- Accessing values of a dictionary
- All combinations of dictionary values
- Avoiding KeyError Exceptions
- Creating an ordered dictionary
- Creating a dictionary
- Dictionaries Example
- Dictionary with default values
- Iterating Over a Dictionary
- Merging dictionaries
- The dict() constructor
- The trailing comma
- Unpacking dictionaries using the ** operator