removing duplicates, or, converting Set() to string

Simon Forman rogue_pedro at yahoo.com
Wed Jul 26 20:10:23 EDT 2006


maphew at gmail.com wrote:
> Hello,
>
> I have some lists for which I need to remove duplicates. I found the
> sets.Sets() module which does exactly this, but how do I get the set
> back out again?
>
> # existing input: A,B,B,C,D
> # desired result: A,B,C,D
>
> import sets
> dupes = ['A','B','B','C','D']
> clean = sets.Set(dupes)
>
> out = open('clean-list.txt','w')
> out.write(clean)
> out.close
>
> ---
> out.write(clean) fails with "TypeError: argument 1 must be string or
> read-only character buffer, not Set" and out.write( str(clean) )
> creates "Set(['A', 'C', 'B', 'D'])" instead of just A,B,C,D.
>
> thanks in advance for your time,
>
> -matt

Do ','.join(clean) to make a single string with commas between the
items in the set.  (If the items aren't all strings, you'll need to
convert them to strings first.)

Peace,
~Simon




More information about the Python-list mailing list