Map class-name strings to integer codes using sorted unique order: build sorted(set(labels)) → dict mapping each class to its index → encode each label via lookup loop. Library: sklearn.preprocessing.LabelEncoder() .fit_transform(labels) assigns codes in sorted alphabetical order, matching the naive approach exactly. RESULT: integer code list.

By hand

labels=['cat','dog','bird','cat','bird','dog','cat']. Sorted unique: ['bird','cat','dog'] → {bird:0, cat:1, dog:2}. Encoded: [1,2,0,1,0,2,1].

naive.py
Replay: real traced execution (multi-file project)
labels = ['cat', 'dog', 'bird', 'cat', 'bird', 'dog', 'cat']
unique = sorted(set(labels))
code_map = {}
for i in range(len(unique)):
    code_map[unique[i]] = i
encoded = []
for lbl in labels:
    encoded.append(code_map[lbl])
print('RESULT:', encoded)
  1. labels ← ['cat', 'dog', 'bird', 'cat', 'bird', 'dog', 'cat']

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

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

    2unique = sorted(set(labels))3code_map = {}4for i in range(len(unique)):
    values this step{}code_map
  4. i ← 0

    3code_map = {}4for i in range(len(unique)):5    code_map[unique[i]] = i
    values this step0i
  5. code_map ← {'bird': 0}

    4for i in range(len(unique)):5    code_map[unique[i]] = i6encoded = []
    values this step{} {'bird': 0}code_map
  6. i ← 1

    3code_map = {}4for i in range(len(unique)):5    code_map[unique[i]] = i
    values this step0 1i
  7. code_map ← {'bird': 0, 'cat': 1}

    4for i in range(len(unique)):5    code_map[unique[i]] = i6encoded = []
    values this step{'bird': 0} {'bird': 0, 'cat': 1}code_map
  8. i ← 2

    3code_map = {}4for i in range(len(unique)):5    code_map[unique[i]] = i
    values this step1 2i
  9. code_map ← {'bird': 0, 'cat': 1, 'dog': 2}

    4for i in range(len(unique)):5    code_map[unique[i]] = i6encoded = []
    values this step{'bird': 0, 'cat': 1} {'bird': 0, 'cat': 1, 'dog': 2}code_map
  10. for i in range(len(unique)):

    3code_map = {}4for i in range(len(unique)):5    code_map[unique[i]] = i
  11. encoded ← []

    5    code_map[unique[i]] = i6encoded = []7for lbl in labels:
    values this step[]encoded
  12. lbl ← 'cat'

    6encoded = []7for lbl in labels:8    encoded.append(code_map[lbl])
    values this step'cat'lbl
  13. encoded ← [1]

    7for lbl in labels:8    encoded.append(code_map[lbl])9print('RESULT:', encoded)
    values this step[] [1]encoded
  14. lbl ← 'dog'

    6encoded = []7for lbl in labels:8    encoded.append(code_map[lbl])
    values this step'cat' 'dog'lbl
  15. encoded ← [1, 2]

    7for lbl in labels:8    encoded.append(code_map[lbl])9print('RESULT:', encoded)
    values this step[1] [1, 2]encoded
  16. lbl ← 'bird'

    6encoded = []7for lbl in labels:8    encoded.append(code_map[lbl])
    values this step'dog' 'bird'lbl
  17. encoded ← [1, 2, 0]

    7for lbl in labels:8    encoded.append(code_map[lbl])9print('RESULT:', encoded)
    values this step[1, 2] [1, 2, 0]encoded
  18. lbl ← 'cat'

    6encoded = []7for lbl in labels:8    encoded.append(code_map[lbl])
    values this step'bird' 'cat'lbl
  19. encoded ← [1, 2, 0, 1]

    7for lbl in labels:8    encoded.append(code_map[lbl])9print('RESULT:', encoded)
    values this step[1, 2, 0] [1, 2, 0, 1]encoded
  20. lbl ← 'bird'

    6encoded = []7for lbl in labels:8    encoded.append(code_map[lbl])
    values this step'cat' 'bird'lbl
  21. encoded ← [1, 2, 0, 1, 0]

    7for lbl in labels:8    encoded.append(code_map[lbl])9print('RESULT:', encoded)
    values this step[1, 2, 0, 1] [1, 2, 0, 1, 0]encoded
  22. lbl ← 'dog'

    6encoded = []7for lbl in labels:8    encoded.append(code_map[lbl])
    values this step'bird' 'dog'lbl
  23. encoded ← [1, 2, 0, 1, 0, 2]

    7for lbl in labels:8    encoded.append(code_map[lbl])9print('RESULT:', encoded)
    values this step[1, 2, 0, 1, 0] [1, 2, 0, 1, 0, 2]encoded
  24. lbl ← 'cat'

    6encoded = []7for lbl in labels:8    encoded.append(code_map[lbl])
    values this step'dog' 'cat'lbl
  25. encoded ← [1, 2, 0, 1, 0, 2, 1]

    7for lbl in labels:8    encoded.append(code_map[lbl])9print('RESULT:', encoded)
    values this step[1, 2, 0, 1, 0, 2] [1, 2, 0, 1, 0, 2, 1]encoded
  26. for lbl in labels:

    6encoded = []7for lbl in labels:8    encoded.append(code_map[lbl])
  27. stdout ← RESULT: [1, 2, 0, 1, 0, 2, 1]

    8    encoded.append(code_map[lbl])9print('RESULT:', encoded)
    values this stepRESULT: [1, 2, 0, 1, 0, 2, 1]stdout

With scikit-learn

LabelEncoder().fit_transform(labels) returns an integer array; .tolist() converts it. classes_ shows the sorted alphabet used for the mapping.

library.py
from sklearn.preprocessing import LabelEncoder
from dalib.display import set_display
set_display()

labels = ['cat', 'dog', 'bird', 'cat', 'bird', 'dog', 'cat']
le = LabelEncoder()
encoded = le.fit_transform(labels).tolist()
print('classes:', le.classes_.tolist())
print('RESULT:', encoded)
classes: ['bird', 'cat', 'dog']
RESULT: [1, 2, 0, 1, 0, 2, 1]

Implementation notes

  • LabelEncoder assigns codes in sorted (alphabetical) order — not insertion order. sorted(set(labels)) in the naive code reproduces this exactly.
  • set ordering is hash-based and NOT deterministic; the sorted() call is required for parity.
  • LabelEncoder is for target labels (y). For input features (X) containing categorical strings, use OrdinalEncoder (single column) or OneHotEncoder (multiple columns) instead.
  • Cross-reference: label-encode (data-cleaning) uses the same sorted-order convention via pandas cat.codes; both sklearn and pandas sort before assigning codes.