Python Practice › String Programs › Program 102
Collapse repeated characters
Write a Python program to replace each group of repeated characters with a single character.
Input
"aabbccdd"
Output
abcd
Approach
dict.fromkeyskeeps one of each in order.
Solution
print("".join(dict.fromkeys("aabbccdd")))Output
abcd
Note
The result keeps the original order, “abcd”. The original sample’s “cabd” came from using a set, whose order is not defined.
Learn the concept
New to this topic? Read the lesson, then come back.