In Post #75, we were introduced to sets as unordered collections of unique items. We saw one way to create them: using the set()
constructor on a list to filter out duplicates. There is another, more direct way to create a set using syntax that might look familiar.
In this post, we’ll cover the two main ways to create a set. Along the way, we’ll uncover a common pitfall that every Python developer learns (usually the hard way!) about creating an empty set.
Method 1: The set()
Constructor
As we saw in our last post, you can create a set from any existing iterable (like a list or a string) by passing it to the set()
function. This is the perfect tool for when you want to convert another collection into a set, often to get rid of duplicates.
# Create a set from a list
my_list = [1, 2, 3, 1]
my_set_from_list = set(my_list)
print(my_set_from_list)
# Create a set from a string
my_string = "hello"
my_set_from_string = set(my_string)
print(my_set_from_string)
The output will be:
{1, 2, 3}
{'e', 'h', 'l', 'o'}
Notice how set("hello")
created a set of the unique characters in the string.
Method 2: Using Curly Braces {}
For creating a new set from scratch with some initial items, you can use curly braces {}
, just like with dictionaries, but you list only the values, without any keys or colons.
# Create a set using the literal {} syntax
my_set = {1, "apple", True, 3.14}
print(my_set)
print(type(my_set))
The output will be:
{1, 3.14, 'apple', True}
<class 'set'>
Even with this syntax, Python enforces uniqueness. If you list an item more than once, it will only be included in the final set a single time.
# Duplicates are automatically ignored during creation
another_set = {1, 2, 2, 3, 3, 3}
print(another_set) # Output: {1, 2, 3}
The Empty Set Gotcha
This is where the syntax gets tricky and can trip you up. We use {}
to create an empty dictionary. So, how do we create an empty set? You might intuitively try this:
# What does this create?
empty_collection = {}
print(type(empty_collection))
The output is: <class 'dict'>
.
This creates an empty dictionary, not an empty set. This is a historical quirk of the language. Dictionaries and their {}
syntax existed in Python long before sets were introduced. To maintain backward compatibility, the language designers kept {}
as the syntax for an empty dictionary.
The Correct Way to Create an Empty Set
This leads to a simple, unbreakable rule:
To create an empty set, you must use the set()
constructor with no arguments.
# The ONLY way to make an empty set
empty_set = set()
print(type(empty_set))
print(empty_set)
The output is now correct:
<class 'set'>
set()
Here is a quick reference to keep in your notes:
To Create | Syntax |
A non-empty set | my_set = {1, 2, 3} |
An empty set | my_set = set() |
An empty dictionary | my_dict = {} |
What’s Next?
You now know the two ways to create a set: using the set()
constructor (especially for creating from iterables or creating an empty set) and using curly braces {}
for creating a non-empty set. Most importantly, you know the critical difference between set()
and {}
for empty collections.
Now that we can confidently create sets, it’s time to learn what makes them so powerful for data analysis: their ability to perform mathematical operations. In Post #77, we’ll dive into the main event and learn how to use union and intersection to combine and compare sets.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com