0
0
PythonProgramBeginner · 2 min read

Python Program to Remove Duplicates from String

You can remove duplicates from a string in Python by using ''.join(dict.fromkeys(your_string)), which keeps characters in order and removes repeats.
📋

Examples

Inputhello
Outputhelo
Inputbanana
Outputban
Input
Output
🧠

How to Think About It

To remove duplicates from a string, think about keeping only the first time each character appears and skipping any repeats. You want to keep the order the same as the original string, so you check each character one by one and add it only if it hasn't been added before.
📐

Algorithm

1
Get the input string.
2
Create an empty structure to remember seen characters.
3
Go through each character in the string.
4
If the character is not seen before, add it to the result and mark it as seen.
5
After checking all characters, return the result string without duplicates.
💻

Code

python
def remove_duplicates(s):
    return ''.join(dict.fromkeys(s))

# Example usage
input_str = 'banana'
print(remove_duplicates(input_str))
Output
ban
🔍

Dry Run

Let's trace the string 'banana' through the code

1

Start with input

s = 'banana'

2

Create dict from keys

dict.fromkeys('banana') creates {'b': None, 'a': None, 'n': None}

3

Join keys to string

''.join(...) results in 'ban'

CharacterSeenResult so far
bNob
aNoba
nNoban
aYesban
nYesban
aYesban
💡

Why This Works

Step 1: Using dict.fromkeys()

The dict.fromkeys() method creates a dictionary with characters as keys, automatically removing duplicates because dictionary keys are unique.

Step 2: Preserving order

Since Python 3.7+, dictionaries keep the order of insertion, so the first occurrence of each character is kept in order.

Step 3: Joining keys back to string

Joining the dictionary keys with ''.join() creates a string with duplicates removed but original order preserved.

🔄

Alternative Approaches

Using a loop and a set
python
def remove_duplicates_loop(s):
    seen = set()
    result = []
    for char in s:
        if char not in seen:
            seen.add(char)
            result.append(char)
    return ''.join(result)

print(remove_duplicates_loop('banana'))
This method is more explicit and works in all Python versions, but is longer to write.
Using collections.OrderedDict
python
from collections import OrderedDict

def remove_duplicates_ordered(s):
    return ''.join(OrderedDict.fromkeys(s))

print(remove_duplicates_ordered('banana'))
This works in older Python versions before 3.7 to preserve order.

Complexity: O(n) time, O(n) space

Time Complexity

The program loops through each character once, so the time grows linearly with the string length.

Space Complexity

Extra space is used to store seen characters and the result, which can be up to the size of the input string.

Which Approach is Fastest?

Using dict.fromkeys() is concise and efficient in modern Python, while the loop method is more explicit but slightly longer.

ApproachTimeSpaceBest For
dict.fromkeys()O(n)O(n)Quick and clean code in Python 3.7+
Loop with setO(n)O(n)Explicit control, works in all Python versions
OrderedDictO(n)O(n)Older Python versions before 3.7
💡
Use dict.fromkeys() to quickly remove duplicates while keeping order in Python 3.7+.
⚠️
Trying to use set() alone removes duplicates but does not keep the original order of characters.