9
votes

This difference is confusing me:

>>> s = "()())()"
>>> print set(s)

set([')', '('])

>>> print {s}

set(['()())()'])

Why?

5
One is a set of characters, the other a set of strings.pault
which is which?Gene Xu
The set() constructor takes an iterable. An string is an iterable, so when you pass the constructor a single string it will iterate over it and each element (i.e. letter) to the set. If you don't want that behavior, pass in a tuple with the single string: set((s,))Mark

5 Answers

14
votes

From the Python documentation for the set() method:

Return a new set object, optionally with elements taken from iterable.

Since a string is an iterable, the set() method creates a set of all characters in the given string. However, since sets do not allow for duplicate values, the output is a set containing the two unique characters in the string: ')' and '('.

On the other hand, the shorthand syntax {s} creates a set out of all items between the curly brackets. Since you only inserted one item s (your string), the output was a set containing only that one item.

6
votes

set() takes an iterable as parameter, whose items will be the elements of the set.

So, set('my string') will contain each character of the iterable I passed it, that is {'m', 'y' ...}

Using {}, you create the set by writing down each of its elements, separated by commas.

{'my string'} contains one element, the string 'my string'

5
votes

When you write:

set(s)

it treats the string as an iterable, and makes a set containing its elements. The elements of a string are the individual characters. So it's equivalent to doing:

{'(', ')', '(', ')', ')', '(', ')'}

Since a set can't contain duplicate elements, you get a set with the two unique characters '(' and ')'.

However, when you write:

{s}

it just makes a set whose element is the value of the variable. The {} syntax treats each variable as a single element, rather than iterating over it.

The set() function is frequently used to convert from one kind of collection to a set of its elements, that's why it iterates over it.

2
votes

Because the first one:

print set(s)

Is simply removing duplicates from the string,

But the second one:

print {s}

Is simply just storing the string in the set, so set with one element is always gonna be one element.

To make the the same result (it could be easier in python 3):

print {i for i in s}
1
votes

The first creates a set which contains only unique values, i.e (). While the other is a string which is what you defined s to be