Create one binary column per category. For each row, the column for its category is 1 and all other columns are 0. By hand, build a per-category indicator list with a nested loop. With pandas, pd.get_dummies() does the same in one call — cast to int for 0/1 output since pandas returns bool by default.

By hand

Build the result as a dict from category name to indicator list. An init loop creates an empty list per category; the outer loop walks the labels and the inner loop appends 1 when s == c and 0 otherwise. The trace shows each category's list growing in lockstep across all 4 rows.

naive.py
Replay: real traced execution (multi-file project)
labels = ['cat', 'dog', 'bird', 'cat']
cats = sorted(set(labels))
result = {}
for c in cats:
    result[c] = []
for s in labels:
    for c in cats:
        result[c].append(1 if s == c else 0)
print('RESULT:', result)
  1. labels ← ['cat', 'dog', 'bird', 'cat']

    1labels = ['cat', 'dog', 'bird', 'cat']2cats = sorted(set(labels))
    values this step['cat', 'dog', 'bird', 'cat']labels
  2. cats ← ['bird', 'cat', 'dog']

    1labels = ['cat', 'dog', 'bird', 'cat']2cats = sorted(set(labels))3result = {}
    values this step['bird', 'cat', 'dog']cats
  3. result ← {}

    2cats = sorted(set(labels))3result = {}4for c in cats:
    values this step{}result
  4. c ← 'bird'

    3result = {}4for c in cats:5    result[c] = []
    values this step'bird'c
  5. result ← {'bird': []}

    4for c in cats:5    result[c] = []6for s in labels:
    values this step{} {'bird': []}result
  6. c ← 'cat'

    3result = {}4for c in cats:5    result[c] = []
    values this step'bird' 'cat'c
  7. result ← {'bird': [], 'cat': []}

    4for c in cats:5    result[c] = []6for s in labels:
    values this step{'bird': []} {'bird': [], 'cat': []}result
  8. c ← 'dog'

    3result = {}4for c in cats:5    result[c] = []
    values this step'cat' 'dog'c
  9. result ← {'bird': [], 'cat': [], 'dog': []}

    4for c in cats:5    result[c] = []6for s in labels:
    values this step{'bird': [], 'cat': []} {'bird': [], 'cat': [], 'dog': []}result
  10. for c in cats:

    3result = {}4for c in cats:5    result[c] = []
  11. s ← 'cat'

    5    result[c] = []6for s in labels:7    for c in cats:
    values this step'cat's
  12. c ← 'bird'

    6for s in labels:7    for c in cats:8        result[c].append(1 if s == c else 0)
    values this step'dog' 'bird'c
  13. result ← {'bird': [0], 'cat': [], 'dog': []}

    7    for c in cats:8        result[c].append(1 if s == c else 0)9print('RESULT:', result)
    values this step{'bird': [], 'cat': [], 'dog': []} {'bird': [0], 'cat': [], 'dog': []}result
  14. c ← 'cat'

    6for s in labels:7    for c in cats:8        result[c].append(1 if s == c else 0)
    values this step'bird' 'cat'c
  15. result ← {'bird': [0], 'cat': [1], 'dog': []}

    7    for c in cats:8        result[c].append(1 if s == c else 0)9print('RESULT:', result)
    values this step{'bird': [0], 'cat': [], 'dog': []} {'bird': [0], 'cat': [1], 'dog': []}result
  16. c ← 'dog'

    6for s in labels:7    for c in cats:8        result[c].append(1 if s == c else 0)
    values this step'cat' 'dog'c
  17. result ← {'bird': [0], 'cat': [1], 'dog': [0]}

    7    for c in cats:8        result[c].append(1 if s == c else 0)9print('RESULT:', result)
    values this step{'bird': [0], 'cat': [1], 'dog': []} {'bird': [0], 'cat': [1], 'dog': [0]}result
  18. for c in cats:

    6for s in labels:7    for c in cats:8        result[c].append(1 if s == c else 0)
  19. s ← 'dog'

    5    result[c] = []6for s in labels:7    for c in cats:
    values this step'cat' 'dog's
  20. c ← 'bird'

    6for s in labels:7    for c in cats:8        result[c].append(1 if s == c else 0)
    values this step'dog' 'bird'c
  21. result ← {'bird': [0, 0], 'cat': [1], 'dog': [0]}

    7    for c in cats:8        result[c].append(1 if s == c else 0)9print('RESULT:', result)
    values this step{'bird': [0], 'cat': [1], 'dog': [0]} {'bird': [0, 0], 'cat': [1], 'dog': [0]}result
  22. c ← 'cat'

    6for s in labels:7    for c in cats:8        result[c].append(1 if s == c else 0)
    values this step'bird' 'cat'c
  23. result ← {'bird': [0, 0], 'cat': [1, 0], 'dog': [0]}

    7    for c in cats:8        result[c].append(1 if s == c else 0)9print('RESULT:', result)
    values this step{'bird': [0, 0], 'cat': [1], 'dog': [0]} {'bird': [0, 0], 'cat': [1, 0], 'dog': [0]}result
  24. c ← 'dog'

    6for s in labels:7    for c in cats:8        result[c].append(1 if s == c else 0)
    values this step'cat' 'dog'c
  25. result ← {'bird': [0, 0], 'cat': [1, 0], 'dog': [0, 1]}

    7    for c in cats:8        result[c].append(1 if s == c else 0)9print('RESULT:', result)
    values this step{'bird': [0, 0], 'cat': [1, 0], 'dog': [0]} {'bird': [0, 0], 'cat': [1, 0], 'dog': [0, 1]}result
  26. for c in cats:

    6for s in labels:7    for c in cats:8        result[c].append(1 if s == c else 0)
  27. s ← 'bird'

    5    result[c] = []6for s in labels:7    for c in cats:
    values this step'dog' 'bird's
  28. c ← 'bird'

    6for s in labels:7    for c in cats:8        result[c].append(1 if s == c else 0)
    values this step'dog' 'bird'c
  29. result ← {'bird': [0, 0, 1], 'cat': [1, 0], 'dog': [0, 1]}

    7    for c in cats:8        result[c].append(1 if s == c else 0)9print('RESULT:', result)
    values this step{'bird': [0, 0], 'cat': [1, 0], 'dog': [0, 1]} {'bird': [0, 0, 1], 'cat': [1, 0], 'dog': [0, 1]}result
  30. c ← 'cat'

    6for s in labels:7    for c in cats:8        result[c].append(1 if s == c else 0)
    values this step'bird' 'cat'c
  31. result ← {'bird': [0, 0, 1], 'cat': [1, 0, 0], 'dog': [0, 1]}

    7    for c in cats:8        result[c].append(1 if s == c else 0)9print('RESULT:', result)
    values this step{'bird': [0, 0, 1], 'cat': [1, 0], 'dog': [0, 1]} {'bird': [0, 0, 1], 'cat': [1, 0, 0], 'dog': [0, 1]}result
  32. c ← 'dog'

    6for s in labels:7    for c in cats:8        result[c].append(1 if s == c else 0)
    values this step'cat' 'dog'c
  33. result ← {'bird': [0, 0, 1], 'cat': [1, 0, 0], 'dog': [0, 1, 0]}

    7    for c in cats:8        result[c].append(1 if s == c else 0)9print('RESULT:', result)
    values this step{'bird': [0, 0, 1], 'cat': [1, 0, 0], 'dog': [0, 1]} {'bird': [0, 0, 1], 'cat': [1, 0, 0], 'dog': [0, 1, 0]}result
  34. for c in cats:

    6for s in labels:7    for c in cats:8        result[c].append(1 if s == c else 0)
  35. s ← 'cat'

    5    result[c] = []6for s in labels:7    for c in cats:
    values this step'bird' 'cat's
  36. c ← 'bird'

    6for s in labels:7    for c in cats:8        result[c].append(1 if s == c else 0)
    values this step'dog' 'bird'c
  37. result ← {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0], 'dog': [0, 1, 0]}

    7    for c in cats:8        result[c].append(1 if s == c else 0)9print('RESULT:', result)
    values this step{'bird': [0, 0, 1], 'cat': [1, 0, 0], 'dog': [0, 1, 0]} {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0], 'dog': [0, 1, 0]}result
  38. c ← 'cat'

    6for s in labels:7    for c in cats:8        result[c].append(1 if s == c else 0)
    values this step'bird' 'cat'c
  39. result ← {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0, 1], 'dog': [0, 1, 0]}

    7    for c in cats:8        result[c].append(1 if s == c else 0)9print('RESULT:', result)
    values this step{'bird': [0, 0, 1, 0], 'cat': [1, 0, 0], 'dog': [0, 1, 0]} {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0, 1], 'dog': [0, 1, 0]}result
  40. c ← 'dog'

    6for s in labels:7    for c in cats:8        result[c].append(1 if s == c else 0)
    values this step'cat' 'dog'c
  41. result ← {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0, 1], 'dog': [0, 1, 0, 0]}

    7    for c in cats:8        result[c].append(1 if s == c else 0)9print('RESULT:', result)
    values this step{'bird': [0, 0, 1, 0], 'cat': [1, 0, 0, 1], 'dog': [0, 1, 0]} {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0, 1], 'dog': [0, 1, 0, 0]}result
  42. for c in cats:

    6for s in labels:7    for c in cats:8        result[c].append(1 if s == c else 0)
  43. for s in labels:

    5    result[c] = []6for s in labels:7    for c in cats:
  44. stdout ← RESULT: {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0, 1], 'dog': [0, 1, 0, 0]}

    8        result[c].append(1 if s == c else 0)9print('RESULT:', result)
    values this stepRESULT: {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0, 1], 'dog': [0, 1, 0, 0]}stdout

With pandas

pd.get_dummies(df['c']) returns a DataFrame with one boolean column per category, in sorted alphabetical order. Chain .astype(int) to convert True/False to 1/0 — pandas 2.x returns bool by default. The snapshot shows dtype: int64 after the cast and the per-column indicator lists.

library.py
import pandas as pd
from dalib.display import set_display
set_display()

labels = ['cat', 'dog', 'bird', 'cat']
df = pd.DataFrame({'c': labels})
ohe = pd.get_dummies(df['c']).astype(int)
result = {c: ohe[c].tolist() for c in ohe.columns}
print('columns:', ohe.columns.tolist())
print('dtype:', ohe.dtypes[0])
print('RESULT:', result)
columns: ['bird', 'cat', 'dog']
dtype: int64
RESULT: {'bird': [0, 0, 1, 0], 'cat': [1, 0, 0, 1], 'dog': [0, 1, 0, 0]}

Implementation notes

  • pd.get_dummies() sorts columns alphabetically — the same order as sorted(set(labels)) in the naive half, so RESULT columns align.
  • The default dtype in pandas 2.x is bool. Add .astype(int) when downstream code expects 0/1 integers rather than True/False.
  • One-hot creates k columns for k categories; label-encode (this chapter) creates a single integer column — both represent the same information but one-hot avoids implying ordinal ranking.
  • Cross-reference: label-encode (this chapter) for the integer-code alternative.