Remove duplicates from a string

This script is one way to remove duplicates from a string, even if they are not consecutive.

This code uses the set object, which cannot contain the same element twice. By converting the string to a set, duplicates disappear.

This works in a context where the order of characters in the original string is not important because Sets are unordered, which means that the items in a set are not stored in any particular order. So after removing duplicates, the characters may appear in a different order compared to that of the original string.

my_string={'A', 'A', 'C', 'B'}
dedup_set= set(my_string)
dedup_string=''.join(dedup_set)
print(dedup_string)

see more articles