Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
They are hash tables, giving average O(1) lookup; keys must be hashable, meaning their hash never changes and equal objects have equal hashes, so immutable types work and lists or dicts do not.
A dict or set computes hash(key) to find a slot, then compares with == to confirm the match, which makes lookups, inserts and deletes O(1) on average. For this to work, an object's hash must stay constant while it is stored, and objects that compare equal must have the same hash. Strings, numbers, tuples of hashable values and frozensets satisfy this; lists, dicts and sets do not. Custom classes are hashable by identity by default, and if you define eq you must also define a consistent hash. Since Python 3.7, dicts preserve insertion order.