How to removing duplicate characters from a string in Python

3 Answers

0 votes
from collections import OrderedDict

s = "pyythoon"

s = "".join(OrderedDict.fromkeys(s))

print(s)


'''
run:

python

'''

 



answered Oct 27, 2019 by avibootz
edited Oct 27, 2019 by avibootz
0 votes
s = "pyythoon"
 
s = "".join(set(s))
 
print(s)
 
 
'''
run:
 
onhytp
 
'''

 



answered Oct 27, 2019 by avibootz
0 votes
s = "pyythoon"
 
s = ''.join(sorted(set(s), key=s.index))
 
print(s)
 
 
'''
run:
 
python
 
'''

 



answered Oct 27, 2019 by avibootz
...