python - How to get rid of duplicate entries in a comma separated string -
i have comma separated string, how remove duplicate entries in string in pythonic way.
for example string "a,a,b"
should changed "a,b"
.
is order of elements important? if not, easiest way create set
:
result = ','.join(set(text.split(',')))
but said, won’t preserve order of original string:
>>> text = 'b,a,b' >>> ','.join(set(text.split(','))) 'a,b'
Comments
Post a Comment