Kodeclik Blog
Declaring and Using Empty Sets in Python
Sets are a data structure in Python that let you store unordered collections of unique items. They are useful when you do not want duplicates—for example, when storing unique IDs, tags, or numbers.
Table of Contents
How to declare an empty set
How to make a set empty
Can we initialize a new set to be equal to an empty set?
What type is an empty set?
Can I use empty braces to create an empty set?
What is the difference between {} and set() in Python?
Why doesn't {} make an empty set?
How do I check if a set is empty?
Summary
How to declare an empty set
The most common way is to call the built-in set() function:
s = set()
print(s) # Output: set()
This creates an empty set that you can later fill with values.
If you want to get a little fancy, starting with Python 3.5 you can create an empty set using set unpacking syntax (PEP 448):
s = {*()}
print(s) # Output: set()
There are other variants that work:
s = {*{}}
s = {*[]}
print(s) # Output: set()
How to make a set empty
If you already have a non-empty set, you can clear it using the clear() method:
s = {1, 2, 3}
s.clear()
print(s) # Output: set()
Alternatively, you could re-assign it to a new empty set:
s = {1, 2, 3}
s = set()
print(s) # Output: set()
Can we initialize a new set to be equal to an empty set?
Indeed you can! You can assign one empty set to another:
s1 = set()
s2 = set(s1)
print(s2) # Output: set()
This is another valid way of initializing a set as empty.
What type is an empty set?
All sets in Python have the type set, regardless of whether they contain integers, strings, or booleans.
s = set()
print(type(s))
# Output: <class 'set'>
Can I use empty braces to create an empty set?
Actually, no, you cannot. In Python, {} creates an empty dictionary, not an empty set.
s = {}
print(type(s))
# Output: <class 'dict'>
So always use set() (or the fancier unpacking trick) when you want an empty set.
What is the difference between {} and set() in Python?
As discussed earlier, in Python, {} creates an empty dictionary, not a set. To create an empty set, always use set(). If you try type({}), the output will be <class 'dict'>, while type(set()) gives <class 'set'>.
Why doesn't {} make an empty set?
The {} syntax is reserved for dictionaries in Python, even though non-empty sets can be defined with curly braces (e.g., {1, 2, 3}). For empty sets, you must use set() to avoid confusion and ensure correct data types.
How do I check if a set is empty?
You can check if a set is empty in several ways. First, you can use boolean context:
if not my_set:
print("Set is empty")
Alternatively, you can use the len() function:
if len(my_set) == 0:
print("Set is empty")
Finally, you can compare it to another empty set:
if my_set == set():
print("Set is empty")
All these methods are commonly used and work reliably.
Summary
- Use set() to declare an empty set.
- {} will give you an empty dictionary, not a set.
- To empty an existing set, use clear() or reassign with set().
- Empty sets have the type set.
- Python >= 3.5 allows alternate literal unpacking tricks like {*()} to create empty sets.
Enjoy this blogpost? Want to learn Python with us? Sign up for 1:1 or small group classes.