Joining Records
Lookup Enrich
Attach a category label to each product by looking up its code in a side
dict. Unknown codes fall back to a default value. The replay shows cat
resolving at each step and result accumulating (name, category) pairs,
with ('mat', 'other') appearing for the unrecognised code 'z'.
By hand
The Pythonic way
A list comprehension folds the lookup and the pairing into one expression.
The get call with its default handles unknown codes inline.
naive.py
products = ['pen', 'cup', 'bag', 'mat']
codes = ['a', 'b', 'b', 'z']
categories = {'a': 'work', 'b': 'home'}
result = []
for name, code in zip(products, codes):
cat = categories.get(code, 'other')
result.append((name, cat))
print('RESULT:', result)
library.py
products = ['pen', 'cup', 'bag', 'mat']
codes = ['a', 'b', 'b', 'z']
categories = {'a': 'work', 'b': 'home'}
result = [(name, categories.get(code, 'other'))
for name, code in zip(products, codes)]
print('RESULT:', result)
RESULT: [('pen', 'work'), ('cup', 'home'), ('bag', 'home'), ('mat', 'other')]
Implementation notes
- This is the everyday "attach a label" pattern — a special case of a left join where the right table is a flat key→value dict rather than a record table. Row count is always preserved because every code produces a value (matched or default).
- The equivalent pandas operation is
df['cat'] = df['code'].map(categories)or.map(categories).fillna('other')— see thepython-pandastrack lessonmap-lookup-columnfor the API form. - Unlike
left-join-with-missing, the fallback is a real label ('other') rather thanNone, so downstream code does not need aNone-guard.