I have a string like 'AABA'. I want to remove multiple occurances by removing others. The result should be 'AB'. Sample Input: AABA Sample Output: AB
Asked
Active
Viewed 603 times
1 Answers
0
If the order doesn't matter, use a set.
word = "AABA"
new_word = "".join(set(word))
If the order DOES matter, use an Ordered Dictionary (from collections library).
from collections import OrderedDict
word = "AABA"
new_word = "".join(OrderedDict.fromkeys(word))
EDIT: Consult the link posted in the comments above - it gives the same advice, but explains it better.

Andrew Bowling
- 206
- 2
- 8
-
2This answer is the same as the answer posted in the duplicate question – Devesh Kumar Singh May 25 '19 at 07:22
-