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
raw = ['  Alice ', 'BOB', ' carol  ', 'DAVE ', ' Eve', 'frank  ']
cleaned = []
for s in raw:
    clean = s.strip().lower()
    cleaned.append(clean)
print('RESULT:', cleaned)

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.