"apple, Banana, APPLE,
cherry , banana , Apple"
# Problems:
# · different case (APPLE vs apple)
# · stray spaces
# · the same fruit repeated
"apple, banana, cherry"
# Goals:
# · one of each (no duplicates)
# · all lowercase, trimmed
# · sorted alphabetically
.split(",") — from Day 16.strip().lower() — from Day 16in + .append() — from Day 18.sort() + ", ".join()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']
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.
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
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
"Apple" and "apple" look different and both survive.not in words instead of not in unique — dedupe against the result, not the input..strip() — a trailing space makes "banana " a different word.Read a line of comma-separated words and print them cleaned, de-duplicated, and sorted — plus a count of how many were removed.
Instead of just removing repeats, report how many times each word appeared, using .count().
students/<your-username>/D19_dedupe.py. Full brief in lab/module_2_practice/D19_dedupe/problem.md. PR before 12:00.
Word Counter mini-project on Day 20.