>> Module 02 / Day 19 / Combine Day

Text + Lists,
Together

Combine day — build a deduplicator
No new syntax today. Instead, we take this week's tools — string methods, slicing, list operations — and aim them at one real problem.
Lecture
75 min · 09:00–10:15
Lab
60 min · Word-list Deduplicator
Module
M2 · Python in Practice
DAY 19 / 40
>> Today's roadmap

One problem, built in stages

  1. The challenge — remove duplicates from a messy word list
  2. Step 1 — split the text into words
  3. Step 2 — normalise each word (the week's string skills)
  4. Step 3 — keep only the first time you see each word
  5. Step 4 — sort and join back into clean output
  6. Lab — build the full deduplicator yourself
>> 01 / 04 · The challenge

From this… to this

input — messy
"apple, Banana, APPLE,
 cherry , banana ,  Apple"

# Problems:
# · different case (APPLE vs apple)
# · stray spaces
# · the same fruit repeated
output — clean
"apple, banana, cherry"

# Goals:
# · one of each (no duplicates)
# · all lowercase, trimmed
# · sorted alphabetically
>> The plan

Four steps, each a tool you already have

STEP 1 · SPLIT
Break the text into words
.split(",") — from Day 16
STEP 2 · NORMALISE
Trim & lowercase each word
.strip().lower() — from Day 16
STEP 3 · DEDUPE
Keep only first-time words
in + .append() — from Day 18
STEP 4 · REBUILD
Sort, then join back
.sort() + ", ".join()
>> 02 / 04 · Split & normalise

Steps 1 & 2 — break apart, then clean

steps_1_2.py
raw = "apple, Banana, APPLE, cherry , banana ,  Apple"

pieces = raw.split(",")        # STEP 1 → ['apple', ' Banana', ...]

words = []
for p in pieces:                # STEP 2
    words.append(p.strip().lower())  # trim + lowercase

print(words)
# → ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple']
>> 03 / 04 · The dedupe trick

Step 3 — "have I seen this before?"

Walk the list and add each word to a result list only if it's not already in it. The membership test in does the checking.

step_3.py
unique = []
for word in words:
    if word not in unique:   # the heart of dedupe
        unique.append(word)    # first time only

print(unique)
# → ['apple', 'banana', 'cherry']  ✅ no repeats
>> 04 / 04 · The whole thing

Step 4 + the complete program

deduplicator.py
def dedupe(raw):
    words = []
    for p in raw.split(","):       # 1 split
        words.append(p.strip().lower())  # 2 normalise

    unique = []
    for w in words:                  # 3 dedupe
        if w not in unique:
            unique.append(w)

    unique.sort()                     # 4 sort + join
    return ", ".join(unique)

print(dedupe("apple, Banana, APPLE, cherry , banana ,  Apple"))
# → apple, banana, cherry
>> Watch out

Where combine-day programs break

  1. Skipping the normalise step — then "Apple" and "apple" look different and both survive.
  2. Checking not in words instead of not in unique — dedupe against the result, not the input.
  3. Forgetting .strip() — a trailing space makes "banana " a different word.
  4. Trying to build and sort in one breath — get each step working first, then chain them.
>> 11:00 – 12:00 · Lab

Today's lab — Word-list Deduplicator

CORE
Build it in four steps

Read a line of comma-separated words and print them cleaned, de-duplicated, and sorted — plus a count of how many were removed.

★ STRETCH
Word frequency

Instead of just removing repeats, report how many times each word appeared, using .count().

>> End of Day 19

What today proved

  • ✓ Break a big problem into small, named steps before writing any code.
  • ✓ The dedupe pattern: build a new list, skipping what's already in it.
  • ✓ String skills and list skills combine into real, useful tools.
01 / 10
←/→ navigate   F fullscreen   Ctrl/⌘+P save as PDF