Python: How to create an empty set and append items to it?

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,

  1. Using set()
  2. 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}

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top