Reading preferences
Concept

Symbol Tables and Sets

Associate keys with values, or remember which keys belong to a collection.

symbol table, map, set, ordered, keys

Labels and membership

Imagine a school cupboard where each labelled box has a price written on it. The label is the key; the price is its value. A symbol table, also called a map, stores key–value associations. Looking up the same key returns its associated value; updating the key replaces that value.

A set asks a different question: which keys are present? It does not need a separate mapped value for each key. Adding the same key twice leaves one distinct member.

Why use them and where they help

Maps help connect names to prices or identifiers to records. Sets help remove duplicate labels or check membership. They describe operations rather than one storage method: a hash table is one possible implementation.

An ordered symbol table also supports operations based on key order, such as the smallest key, predecessor, or keys within a range. A balanced search tree can provide ordered operations; an unordered hash table does not automatically provide them. Costs depend on the implementation.

Follow an example

ActionMapSet of labels
Add apple with price 3apple → 3apple
Add pear with price 5apple → 3, pear → 5apple, pear
Change apple to 4apple → 4, pear → 5apple, pear

The set still has two members. Changing apple's mapped value does not create a second apple key.

See the reasoning

Follow the example one step at a time

Compare changing a map value with set membership. This is a prepared teaching demonstration, not live code execution.

Start

map: empty   set: empty

The map stores values for keys; the set stores distinct keys.

Python illustration

prices = {"apple": 3, "pear": 5}
prices["apple"] = 4
labels = {"apple", "pear", "apple"}
print("apple:", prices["apple"])
print("members:", len(labels))
print("pear present:", "pear" in labels)
print("sorted keys:", sorted(prices))

Prepared output — not live execution:

apple: 4
members: 2
pear present: True
sorted keys: ['apple', 'pear']

Python dictionaries preserve insertion order, not sorted key order. sorted(prices) makes a separate sorted list, and requires mutually comparable keys. Dict and set keys must be hashable. A missing dictionary key accessed with prices[key] raises KeyError; .get() can return None for both a missing key and a present key with value None, so use in to distinguish them.

Time, space, and common mistakes

With suitable hashing assumptions, dictionary and set membership, insertion, and deletion are expected O(1), but worst-case lookup can be O(n). Storing n distinct entries uses O(n) space. A balanced search tree can support ordered lookup and updates in O(log n), with range reporting also depending on the number returned. Sorting dictionary keys takes O(n log n) worst-case time and O(n) output space; insertion order alone is not sorted order.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. What does a symbol table associate with each key?
2. What does a set record?
3. What can an ordered symbol table support?
4. Why might a membership check be needed when a dictionary stores None?

References and further reading