This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Creating set with empty string returns empty set
Type: behavior Stage: resolved
Components: Versions: Python 3.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: EmilBode, ammar2
Priority: normal Keywords:

Created on 2019-09-30 15:19 by EmilBode, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg353585 - (view) Author: Emil Bode (EmilBode) Date: 2019-09-30 15:19
Initializing/creating a new set with an empty string via the set-command returns an empty set instead of set with the empty string.

As in:
While set('somestring') gives me a set of size one, 
set('') gives me an empty set (of size zero), just as set() would do

It is possible to create a set with an empty string, as the command {''} and set(['']) work just as expected
msg353586 - (view) Author: Emil Bode (EmilBode) Date: 2019-09-30 15:22
Some details about my setup:
Python 3.7.1,
Spyder 3.3.2
IPython 7.2.0
under Windows 10 64-bit
msg353587 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2019-09-30 15:27
I think the key thing you're missing here is that the set() constructor can take any arbitrary iterable ( It simply goes over all the elements inside and adds them all to the set. This is no different than the following for example:

    >>> for char in 'somestring': 
    ...   print(char)             
    >>> for char in '':
    ...   print(char)

When you iterate over a string, it simply goes over each character inside it.

> While set('somestring') gives me a set of size one

This should not be the case, set('something') should be giving you a set of size 9. Each element in the set being a character from the string 'somestring'

    >>> set('somestring')
    {'t', 's', 'e', 'n', 'g', 'o', 'r', 'i', 'm'}
    >>> len(set('somestring'))

> set('') gives me an empty set (of size zero)

Now hopefully it should be obvious why this happens, the set() constructor goes over each character in the string. Which in this case, there aren't any because the string is completely empty. This leads to an empty set. This is no different than doing set([])
msg353588 - (view) Author: Emil Bode (EmilBode) Date: 2019-09-30 15:31
You're right, I tested with set('a'), which gave me a set of size one, but generalized it here to 'somestring'.
Maybe I'm just too loose with using set, instead of {}

Sorry to bother you
Date User Action Args
2022-04-11 14:59:21adminsetgithub: 82508
2019-09-30 15:31:25EmilBodesetmessages: + msg353588
2019-09-30 15:27:37ammar2setstatus: open -> closed

nosy: + ammar2
messages: + msg353587

resolution: not a bug
stage: resolved
2019-09-30 15:22:24EmilBodesetmessages: + msg353586
2019-09-30 15:19:05EmilBodecreate