Clean a list of messy strings by stripping leading/trailing whitespace and converting to lowercase. The trace shows clean resolving to the normalised value each iteration before being appended to cleaned.

By hand

Iterate over the raw strings. For each one, assign clean = s.strip().lower() to make the two-step cleaning explicit in the trace, then append to cleaned.

naive.py
Replay: real traced execution (multi-file project)
raw = ['  Alice ', 'BOB', ' carol  ', 'DAVE ', ' Eve', 'frank  ']
cleaned = []
for s in raw:
    clean = s.strip().lower()
    cleaned.append(clean)
print('RESULT:', cleaned)
  1. raw ← [' Alice ', 'BOB', ' carol ', 'DAVE ', ' Eve', 'frank ']

    1raw = ['  Alice ', 'BOB', ' carol  ', 'DAVE ', ' Eve', 'frank  ']2cleaned = []
    values this step[' Alice ', 'BOB', ' carol ', 'DAVE ', ' Eve', 'frank ']raw
  2. cleaned ← []

    1raw = ['  Alice ', 'BOB', ' carol  ', 'DAVE ', ' Eve', 'frank  ']2cleaned = []3for s in raw:
    values this step[]cleaned
  3. s ← ' Alice '

    2cleaned = []3for s in raw:4    clean = s.strip().lower()
    values this step' Alice 's
  4. clean ← 'alice'

    3for s in raw:4    clean = s.strip().lower()5    cleaned.append(clean)
    values this step'alice'clean
  5. cleaned ← ['alice']

    4    clean = s.strip().lower()5    cleaned.append(clean)6print('RESULT:', cleaned)
    values this step[] ['alice']cleaned
  6. s ← 'BOB'

    2cleaned = []3for s in raw:4    clean = s.strip().lower()
    values this step' Alice ' 'BOB's
  7. clean ← 'bob'

    3for s in raw:4    clean = s.strip().lower()5    cleaned.append(clean)
    values this step'alice' 'bob'clean
  8. cleaned ← ['alice', 'bob']

    4    clean = s.strip().lower()5    cleaned.append(clean)6print('RESULT:', cleaned)
    values this step['alice'] ['alice', 'bob']cleaned
  9. s ← ' carol '

    2cleaned = []3for s in raw:4    clean = s.strip().lower()
    values this step'BOB' ' carol 's
  10. clean ← 'carol'

    3for s in raw:4    clean = s.strip().lower()5    cleaned.append(clean)
    values this step'bob' 'carol'clean
  11. cleaned ← ['alice', 'bob', 'carol']

    4    clean = s.strip().lower()5    cleaned.append(clean)6print('RESULT:', cleaned)
    values this step['alice', 'bob'] ['alice', 'bob', 'carol']cleaned
  12. s ← 'DAVE '

    2cleaned = []3for s in raw:4    clean = s.strip().lower()
    values this step' carol ' 'DAVE 's
  13. clean ← 'dave'

    3for s in raw:4    clean = s.strip().lower()5    cleaned.append(clean)
    values this step'carol' 'dave'clean
  14. cleaned ← ['alice', 'bob', 'carol', 'dave']

    4    clean = s.strip().lower()5    cleaned.append(clean)6print('RESULT:', cleaned)
    values this step['alice', 'bob', 'carol'] ['alice', 'bob', 'carol', 'dave']cleaned
  15. s ← ' Eve'

    2cleaned = []3for s in raw:4    clean = s.strip().lower()
    values this step'DAVE ' ' Eve's
  16. clean ← 'eve'

    3for s in raw:4    clean = s.strip().lower()5    cleaned.append(clean)
    values this step'dave' 'eve'clean
  17. cleaned ← ['alice', 'bob', 'carol', 'dave', 'eve']

    4    clean = s.strip().lower()5    cleaned.append(clean)6print('RESULT:', cleaned)
    values this step['alice', 'bob', 'carol', 'dave'] ['alice', 'bob', 'carol', 'dave', 'eve']cleaned
  18. s ← 'frank '

    2cleaned = []3for s in raw:4    clean = s.strip().lower()
    values this step' Eve' 'frank 's
  19. clean ← 'frank'

    3for s in raw:4    clean = s.strip().lower()5    cleaned.append(clean)
    values this step'eve' 'frank'clean
  20. cleaned ← ['alice', 'bob', 'carol', 'dave', 'eve', 'frank']

    4    clean = s.strip().lower()5    cleaned.append(clean)6print('RESULT:', cleaned)
    values this step['alice', 'bob', 'carol', 'dave', 'eve'] ['alice', 'bob', 'carol', 'dave', 'eve', 'frank']cleaned
  21. for s in raw:

    2cleaned = []3for s in raw:4    clean = s.strip().lower()
  22. stdout ← RESULT: ['alice', 'bob', 'carol', 'dave', 'eve', 'frank']

    5    cleaned.append(clean)6print('RESULT:', cleaned)
    values this stepRESULT: ['alice', 'bob', 'carol', 'dave', 'eve', 'frank']stdout

The Pythonic way

A list comprehension chains .strip().lower() directly on each element. Methods can be chained in any order; stripping before lowering is idiomatic (whitespace is case-insensitive so order does not matter here).

library.py
raw = ['  Alice ', 'BOB', ' carol  ', 'DAVE ', ' Eve', 'frank  ']
cleaned = [s.strip().lower() for s in raw]
print('RESULT:', cleaned)
RESULT: ['alice', 'bob', 'carol', 'dave', 'eve', 'frank']

Implementation notes

  • .strip() removes both leading and trailing whitespace (spaces, tabs, newlines). Use .lstrip() or .rstrip() to remove from one side only.
  • This pattern is typically applied after parsing a CSV column: read raw strings with csv.reader or csv.DictReader, then normalise with a comprehension before further processing.
  • For case-insensitive comparison without permanent lowercasing, use s.casefold() instead of .lower() — it handles Unicode edge cases (e.g. German ß) more correctly.