Python: Dictionaries
A mutable key-value pair data structure. Keys are unique and must be of an immutable type. Numbers that compare equal (e.g. 1 and 1.0) can be used interchangeably as indexes. (Floats keys are bad practice though.)
dict() or {}
- Create an empty dictionary.
{KEY1: VALUE1, KEY2: VALUE2, ...}
- Create a dicionary with key-value pairs pre-populated.
- Using dict(VALUE: KEY,…) throws a
SyntaxError.
dict[KEY]
- Retrieve the value at index KEY.
dict[KEY] = VALUE
- Assign VALUE to the index KEY.
dict[NON_EXISTENT_KEY]
- A
KeyErroris thrown.
KEY in dict or KEY not in dict
- Check whether a KEY existing in a dict.
- Returns boolean.
Methods
del dict[KEY]
- Remove KEY (and value) from a dictionary.
- Raises a
KeyErrorif KEY not found indict.
dict.clear()
- Remove all key-value pairs from the dictionary.
dict.copy()
- Return a shallow copy of the dictionary.
dict.fromkeys(ITER[, VALUE])
- Create a dictionary, with keys set to values of ITER, and values set to
Noneor VALUE (if specified).
dict.get(KEY[, DEFAULT])
- Return the value for key, if key is in the dict, else returns
DEFAULT. IfDEFAULTis not given, this method never raisesKeyError.
dict.items()
- Return a new view of the dictionary key-value pairs.
- Essentially a list of sets. The sets being key-value pairs.
- Useful for iterating over a dict in loops.
dict.keys()
- Return a new view of the dictionary keys.
- Essentially a list of dictionary keys.
dict.pop(KEY[, DEFAULT])
- If KEY is in the dict, remove it, and return its value; else, return DEFAULT. If DEFAULT is not
given, and key, is not in the dict, a
KeyErroris raised.
dict.popitem()
- Remove and return a key-value pair from the dict.
- Returned as a tuple. LIFO is guaranteed as of Python 3.7.
dict.setdefault(KEY[, DEFAULT])
- If KEY is in the dict, return its value; else, insert KEY with a value of DEFAULT and return DEFAULT.
- DEFAULT defaults to
None.
dict.update(ARGS)
- Update the dict with the key/value pairs from ARGs.
- Return
None. - ARGs is either another dictionary or an iterable with two values (e.g. tuple or list with a length of two).
dict.values()
- Return a new view of the dictionary’s values.
- Essentially a list of dictionary values.
iter(dict)
- Return an iterator over the keys of the dictionary.
len(dict)
- Return the number of items (keys) in the dictionary.
list(dict)
- Returns a list of all the keys in a dictionary.
reversed(dict)
- Return an ITER object, containing the reversed keys of the dict. Mainly for use in loops.
- Python 3.8+.
Dictionary comparison
Needs adding. Caveats. See link at top below method section
Dictionary view objects
Needs adding. They are dynamic and update.
Looping
Big-O
Needs adding.
Valid keys
Keys must be hashable. An object is hashable if it has a value does not change during its lifetime.
Immutable containers (e.g. tuples and frozensets) are only hashable if their elements are hashable.
Objects which are instances of user-defined classes are also hashable by default. They all
compare unequal (except with themselves), and their hash value is derived from id().