Skip to content
Advertisement

Python: Check for unique characters on a String

I’m asking the user to input a keyword and then remove any duplicate characters.

Example:

Input: balloon

Output: balon

I’ve tried this solution: List of all unique characters in a string? but it marks it as a syntax error.

Any ideas?

Advertisement

Answer

For your answer, order is important. Here is a one line solution:

word = raw_input()
reduced_word = ''.join(
    [char for index, char in enumerate(word) if char not in word[0:index]])
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement