Dictionary Keys, Values, and Items

A dictionary view can traverse its labels, its stored values, or pairs of both. Pick a view that preserves the evidence a caller needs.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Follow two equal values
  2. Keep reverse lookup deliberate
  3. Trace which position survives a projection
  4. Estimate the cost of the right membership operation

Follow two equal values

A map from both A and B to seven has two keys and two values. A values view yields both sevens. An items view retains which seven belongs to which key. See frequency maps for when several keys can have the same count.

Keep reverse lookup deliberate

Testing whether a code is a key uses key membership. Testing whether a value occurs needs a values check, or a separately maintained reverse structure. A reverse index can also retain all keys matching a repeated value.[1]

Trace which position survives a projection

In {"A": 8, "B": 8, "C": 2}, keys are unique while the values include two eights. Iterating over the dictionary yields A, B and C in insertion order in Python. The items view retains which labels own the two equal values. Construct a report from items when both identity and amount are required. See reversing a mapping.

Estimate the cost of the right membership operation

A lookup can check whether a key is present using the dictionary's hash index. A test asking whether any record has value eight generally needs to inspect values or maintain a reverse index. Such an index should be updated when a record changes or is removed. Test updating A from eight to six while B remains at eight. See dictionary lookups and live views and snapshots.

References

  1. Python Software Foundation, Python Standard Library: Built-in Types ↩