["python"]
Removing duplicate characters from a string

How can I remove duplicate characters from a string using Python? For example, let's say I have a string: foo = 'mppmt' How can I make the string: foo = 'mpt' NOTE: Order is not important
If order does not matter, you can use "".join(set(foo)) set() will create a set of unique letters in the string, and "".join() will join the letters back to a string in arbitrary order. If order does matter, you can use a dict instead of a set, which since Python 3.7 preserves the insertion order of the keys. (In the CPython implementation, this is already supported in Python 3.6 as an implementation detail.) foo = "mppmt" result = "".join(dict.fromkeys(foo)) resulting in the string "mpt". In earlier versions of Python, you can use collections.OrderedDict, which has been available starting from Python 2.7.