Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing duplicate characters from a string

Tags:

python

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

like image 946
JSW189 Avatar asked Mar 23 '12 14:03

JSW189


People also ask

How do I remove duplicates from a string in Java 8?

You can use the Stream. distinct() method to remove duplicates from a Stream in Java 8 and beyond. The distinct() method behaves like a distinct clause of SQL, which eliminates duplicate rows from the result set.


2 Answers

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.

like image 184
Sven Marnach Avatar answered Sep 17 '22 13:09

Sven Marnach


If order does matter, how about:

>>> foo = 'mppmt' >>> ''.join(sorted(set(foo), key=foo.index)) 'mpt' 
like image 40
DSM Avatar answered Sep 18 '22 13:09

DSM