In this article, first we will discuss different ways to create an empty set and then we will see how to add or append items to the empty set.
In python there are two ways to create an empty set,
 Using set()
 Using empty set literal
Let’s discuss both of them one by one,
Creating an empty set in python using set()
In Python, Set class provides a constructor to create a set object i.e.
Set([iterable])
It accepts an optional iterable sequence and returns a set, initialized with the elements in the sequence. But if we don’t pass any iterable sequence in the constructor then it returns an empty set. For example,
# Create an empty set by creating a set class object my_set = set() print(my_set)
Output:
set()
Here we created an empty set using set(). We can confirm if set is empty by checking its size,
print('Set Size: ', len(my_set))
Output:
Set Size: 0
We can also confirm if returned object is a set object or not, by printing its type,
print(type(my_set))
Output:
<class 'set'>
Creating an empty set in python using empty set literal
In python empty curly brackets i.e. {} are mainly used to create a dictionary. But since python 3.5, if we pass some comma separated arguments in curly square brackets then it will create a set with it. For example: {1, 2, 3} can be used to create a set with 3 values in it.
So, to create an empty set, we can use the same technique i.e.
# Create an empty set using literal {} # and unpacking empty tuple in it (Available in Python >= 3.5) my_set = {*()} print(my_set)
Output:
set()
Here we unpacked an empty tuple in the curly square brackets and it returned an empty set.
We can confirm if set is empty by checking its size,
print('Set Size: ', len(my_set))
Output:
Set Size: 0
We can also confirm if the returned object is a set object or not, by printing its type,
print(type(my_set))
Output:
<class 'set'>
So, these were the two ways to create an empty set. So, now let’s see how to add items to an empty set.
Adding an item to an empty set
To add an item to an empty set, we can use the add() function of set,
# Creating an empty set new_set = set() # Adding a character to the empty set new_set.add('a') # Adding a number to the set new_set.add('a') print(new_set)
Output:
{'a'}
add() function accepts an element as an argument and adds that to the set.
In the above example, we created an empty set and then called the add() function two times on the set object, to add two items to the set.
Adding multiple items to an empty set
We can add multiple items to an empty set, in a single line using update() function of the set,
# Creating an empty set new_set = set() # Adding multiple elements to an empty set new_set.update((11, 'b', 'Hello')) print(new_set)
Output:
{'Hello', 11, 'b'}
update() function accepts a single or multiple iterable sequences as arguments and adds all the items in these sequences to the set. So, in the above example we created an empty set and then added all the items in a tuple to the empty set, that too in a single line.
So, this is how we can create an empty set and add items to it.
The complete example is as follows,
def main(): print('***** 2 Ways to create an empty Set Literal *****') print('*** Creating an empty set using set() ***') # Create an empty set by creating a set class object my_set = set() print('Empty Set: ') print(my_set) print('Check type of empty set: ') print(type(my_set)) print('Check size of empty set: ') print('Set Size: ', len(my_set)) print('*** Creating an empty set using {} ***') # Create an empty set using literal {} # and unpacking empty tuple in it (Available in Python >= 3.5) my_set = {*()} print('Empty Set: ') print(my_set) print('Check type of empty set: ') print(type(my_set)) print('Check size of empty set: ') print('Set Size: ', len(my_set)) print("**** Add elements to an empty set ****") print("Add element to an empty set using set.add() function") # Creating an empty set new_set = set() # Adding a character to the empty set new_set.add('a') # Adding a number to the set new_set.add('a') print('Set Contents: ') print(new_set) print("Add multiple elements to an empty set using set.update()") # Creating an empty set new_set = set() # Adding multiple elements to an empty set new_set.update((11, 'b', 'Hello')) print('Set Contents: ') print(new_set) if __name__ == '__main__': main()
Output:
***** 2 Ways to create an empty Set Literal ***** *** Creating an empty set using set() *** Empty Set: set() Check type of empty set: <class 'set'> Check size of empty set: Set Size: 0 *** Creating an empty set using {} *** Empty Set: set() Check type of empty set: <class 'set'> Check size of empty set: Set Size: 0 **** Add elements to an empty set **** Add element to an empty set using set.add() function Set Contents: {'a'} Add multiple elements to an empty set using set.update() Set Contents: {'b', 'Hello', 11}
Pandas Tutorials Learn Data Analysis with Python

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most soughtafter professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.