Python Practice › String Programs › Program 35
Remove duplicate characters
Write a Python program to remove duplicate characters, keeping the first occurrence of each.
Input
"sqatools"
Output
sqatol
Approach
dict.fromkeyskeeps the first occurrence in order.
Solution
text = "sqatools"print("".join(dict.fromkeys(text)))Output
sqatol
Line by line
Line 2
Dictionary keys are unique and keep insertion order, so joining them gives the de-duplicated string.
Learn the concept
New to this topic? Read the lesson, then come back.