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?
I wrote a method that handles the following edge-cases:
", ".join({'abc'})
will return"a, b, c"
. My desired output was"abc"
.""
Sets don't have a
join
method but you can usestr.join
instead.The
str.join
method will work on any iterable object including lists and sets.Note: be careful about using this on sets containing integers; you will need to convert the integers to strings before the call to join. For example
You have the join statement backwards try:
The
join
is called on the string:I think you just have it backwards.
The
join
is a string method, not a set method.