Python: how to join entries in a set into one string?

Spencer picture Spencer · Sep 6, 2011 · Viewed 120.1k times · Source

Basically, I am trying to join together the entries in a set in order to output one string. I am trying to use syntax similar to the join function for lists. Here is my attempt:

list = ["gathi-109","itcg-0932","mx1-35316"]
set_1 = set(list)
set_2 = set(["mx1-35316"])
set_3 = set_1 - set_2
print set_3.join(", ")

However I get this error: AttributeError: 'set' object has no attribute 'join'

What is the equivalent call for sets?

Answer

Jmjmh picture Jmjmh · Sep 6, 2011
', '.join(set_3)

The join is a string method, not a set method.