Empty set literal?
By all means, please use set()
to create an empty set.
But, if you want to impress people, tell them that you can create an empty set using literals and *
with Python >= 3.5 (see PEP 448) by doing:
>>> s = {*()} # or {*{}} or {*[]}>>> print(s)set()
this is basically a more condensed way of doing {_ for _ in ()}
, but, don't do this.
Just to extend the accepted answer:
From version 2.7
and 3.1
python has got set
literal {}
in form of usage {1,2,3}
, but {}
itself still used for empty dict.
Python 2.7 (first line is invalid in Python <2.7)
>>> {1,2,3}.__class__<type 'set'>>>> {}.__class__<type 'dict'>
Python 3.x
>>> {1,2,3}.__class__<class 'set'>>>> {}.__class__<class 'dict'>
More here: https://docs.python.org/3/whatsnew/2.7.html#other-language-changes