97

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?

Spencer
  • 21,348
  • 34
  • 85
  • 121

8 Answers8

190
', '.join(set_3)

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

axel22
  • 32,045
  • 9
  • 125
  • 137
Jmjmh
  • 2,016
  • 1
  • 13
  • 11
36

Sets don't have a join method but you can use str.join instead.

', '.join(set_3)

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

set_4 = {1, 2}
', '.join(str(s) for s in set_4)
Mike Graham
  • 73,987
  • 14
  • 101
  • 130
Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77
5

Set's do not have an order - so you may lose your order when you convert your list into a set, i.e.:

>>> orderedVars = ['0', '1', '2', '3']
>>> setVars = set(orderedVars)
>>> print setVars
('4', '2', '3', '1')

Generally the order will remain, but for large sets it almost certainly won't.

Finally, just incase people are wondering, you don't need a ', ' in the join.

Just: ''.join(set)

:)

John
  • 51
  • 1
  • 1
5

The join is called on the string:

print ", ".join(set_3)
MByD
  • 135,866
  • 28
  • 264
  • 277
4

Nor the set nor the list has such method join, string has it:

','.join(set(['a','b','c']))

By the way you should not use name list for your variables. Give it a list_, my_list or some other name because list is very often used python function.

Ski
  • 14,197
  • 3
  • 54
  • 64
4

You have the join statement backwards try:

print ', '.join(set_3)
Hunter
  • 107
  • 2
3

I think you just have it backwards.

print ", ".join(set_3)
recursive
  • 83,943
  • 34
  • 151
  • 241
0

I wrote a method that handles the following edge-cases:

  • Set size one. A ", ".join({'abc'}) will return "a, b, c". My desired output was "abc".
  • Set including integers.
  • Empty set should returns ""
def set_to_str(set_to_convert, separator=", "):
        set_size = len(set_to_convert)
        if not set_size:
            return ""
        elif set_size == 1:
            (element,) = set_to_convert
            return str(element)
        else:
            return separator.join(map(str, set_to_convert))
  • Could have used `" ".join({'abc'})` to get the desired o/p `"abc"`. [executable code in repl.it](https://repl.it/@abhijeetbhanjadeo/settostr) – Abhijeet Dec 05 '18 at 02:55
  • Interesting. Just realized that set('abc') and {'abc'} have different behaviors. That's what threw me off. – Fernando Irarrázaval G Dec 05 '18 at 15:06
  • I have updated the [executable code](https://repl.it/@abhijeetbhanjadeo/settostr) to operate for [set](https://docs.python.org/2/tutorial/datastructures.html#sets) & [dictionary](https://docs.python.org/2/tutorial/datastructures.html#dictionaries) – Abhijeet Dec 07 '18 at 09:12
  • FYI, {'abc'} is a set. And `"".join(set(ip_data))` gives a randomized order of the letters (example, 'bca') because set creates three elements {'a', 'b', 'c'} but does not preserves order. – Fernando Irarrázaval G Dec 07 '18 at 15:04