Free lab Real Python 3. Zero installs. Your code stays in this browser. Open the playground

Cleaning Messy Data

14 min 35 XP
Study loop Read Predict Run Tweak Prove Review
Lesson 2 of 5 · View course roadmap
Step 1

Learn the idea

Real datasets arrive filthy: stray spaces, empty cells, "n/a", numbers stored as text. Analysts joke that the job is 80% cleaning — and the pattern is always the same funnel:

  • value.strip() — cut the whitespace first
  • Skip empties early: if not value: continue
  • Convert inside a safety net: try: int(value) except ValueError: skip

The golden rule: never let one bad value kill the whole run. Quarantine what you can't parse, keep what you can, and (in real jobs) count what you skipped — a spike in bad rows is itself a finding.

Where you'll use this

Analysts genuinely spend most of their time here: exported CSVs with stray spaces, 'N/A', euro signs and thousands separators. Robust cleaning is why senior analysts' numbers are trusted.

Common mistakes

  • Cleaning while iterating the same list you're modifying — build a new clean list instead.
  • int("3.5") raises ValueError — parse decimals with float() first if decimals are possible.
  • Silently dropping bad rows without counting them — in real work, the skip count is itself a data-quality metric.

Pro tip

Normalise aggressively before converting: value.strip().lower().replace(",", "").removeprefix("€") handles most European CSV horrors in one chain.

Step 2

Try it yourself

Blank · autosaved

The lesson example is loaded and ready — press Run, then change something and run it again. Breaking it is part of learning. Want a clean slate? Tap “New blank”.

PYexample.py
+ Enter to run
Output appears here…
Step 3

Pass the challenge +35 XP

Blank · autosaved

Clean raw = [" 42 ", "17", "oops", "", "23 ", "n/a", "8"] into a list of ints, silently skipping anything that isn't a number. Print the clean list, then its sum.

Target output
[42, 17, 23, 8]
90
PYchallenge.py
Run your code to check it…
Step 4

Check your understanding

1. What does " 42 ".strip() return?
2. Why wrap int(value) in try/except when cleaning?
Last step

Your notes (saved on this device)

Tip: use and to move between lessons, K to search everything.

Your next ten minutes

Write Python that does something useful.

Start free. No install, no card, no passive video marathon.

Start learning free → Explore the path