Python: How to Create an Empty Set and Append Items to It?

A Set is an unordered collection data type that can be iterated, mutated, and does not contain duplicate elements. Python’s set class represents the mathematical concept of a set. The main advantage of using a set over a list is that it has a highly optimised method for determining whether a specific element is contained in the set. This is based on the hash table data structure. Because sets are unordered, we cannot access items using indexes as we do in lists.

Characteristics of set:

  • Sets are not in any particular order.
  • Set elements are one of a kind. It is not permitted to use duplicate components.
  • A set’s elements can be modified, but the set’s elements must be of an immutable form.

In this article, we will first look at various ways to create an empty set, and then we will look at how to add or append items to the empty set.

Methods to Create an Empty Set

Method #1: Using set() to create an empty set in Python

A set is formed by enclosing all of the items (elements) within curly braces, separated by commas, or by using the built-in set() function.

It can contain an unlimited number of items of various types (integer, float, tuple, string etc.). A set, on the other hand, cannot have mutable elements such as lists, sets, or dictionaries as its elements.

It takes an optional iterable sequence and returns a set that has been initialised with the elements in the sequence. However, if no iterable sequence is passed to the function it returns an empty set.

Below is the implementation:

# creating new set
emptyset = set()
# printing the set
print(emptyset)
# printing the size of empty set
print("length of set = ", len(emptyset))

Output:

set()
length of set =  0

Method #2:Using empty set literal

Empty curly brackets, i.e., are commonly used in Python to create a dictionary. However, starting with Python 3.5, if we pass some comma separated arguments in curly square brackets, it will create a set with them. For example, 1, 2, 3 can be used to make a set with three values.

So, to make an empty set, we can use the same method.

Below is the implementation:

# creating new set
emptyset = {*()}
# printing the set
print(emptyset)
# printing the size of empty set
print("length of set = ", len(emptyset))

Output:

set()
length of set =  0

Append an item to an empty set

In set, insertion is accomplished via the set.add() function, which generates an appropriate record value for storage in the hash table. The same as searching for a specific item, i.e., O(1) on average. However, in the worst-case scenario, it can become O (n).

Below is the implementation:

# creating new set
emptyset = {*()}
# adding elements to set
emptyset.add('BTechGeeks')
emptyset.add('Hello')
emptyset.add('BTechGeeks')
# printing the set
print(emptyset)

Output:

{'BTechGeeks', 'Hello'}

Appending multiple items to the empty set

Using the set’s update() function, we can add multiple items to an empty set in a single line.

Below is the implementation:

# creating new set
emptyset = set()
# adding elements to set
emptyset.update(('Hello', 'BTechGeeks', 'Hello'))
# printing the set
print(emptyset)

Output:

{'Hello', 'BTechGeeks'}

Explanation:

The update() function takes a single or several iterable sequences as arguments and updates the set with all of the things in those sequences. In the preceding example, we created an empty set and then appended all of the items in a tuple to it in a single line.

 
Related Programs: