Python - Removing duplicates from a string
Posted
by Daniel
on Stack Overflow
See other posts from Stack Overflow
or by Daniel
Published on 2010-05-19T11:50:43Z
Indexed on
2010/05/19
12:10 UTC
Read the original article
Hit count: 192
def remove_duplicates(strng):
"""
Returns a string which is the same as the argument except only the
first occurrence of each letter is present. Upper and lower case
letters are treated as different. Only duplicate letters are removed,
other characters such as spaces or numbers are not changed.
>>> remove_duplicates('apple')
'aple'
>>> remove_duplicates('Mississippi')
'Misp'
>>> remove_duplicates('The quick brown fox jumps over the lazy dog')
'The quick brown fx jmps v t lazy dg'
>>> remove_duplicates('121 balloons 2 u')
'121 balons 2 u'
"""
s = strng.split()
return strng.replace(s[0],"")
Writing a function to get rid of duplicate letters but so far have been playing around for an hour and can't get anything. Help would be appreciated, thanks.
© Stack Overflow or respective owner