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 trace shows cat resolving at each step and result accumulating (name, category) pairs, with ('mat', 'other') appearing for the unrecognised code 'z'.

By hand

Iterate over products and codes together. For each pair call categories.get(code, 'other') to resolve the label, then append the (name, cat) tuple to result.

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)

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.

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 the python-pandas track lesson map-lookup-column for the API form.
  • Unlike left-join-with-missing, the fallback is a real label ('other') rather than None, so downstream code does not need a None-guard.