Remove duplicate words
Write a Python program to remove duplicate words (ignoring case) and keep the first occurrence.
"John jany sabi row john sabi"
John jany sabi row
Approach
- Track lower-cased words already seen.
Solution
text = "John jany sabi row john sabi"seen = set()result = []for w in text.split(): if w.lower() not in seen: seen.add(w.lower()) result.append(w)print(" ".join(result))John jany sabi row
Line by line
Comparing lower-cased words treats “John” and “john” as the same.
Learn the concept
New to this topic? Read the lesson, then come back.