Summary: In this tutorial, we will learn what set is in Python, what different operations we can perform on it, and when we should use it in our Python program.

Introduction to Python Set

The set is one of the data structures that comes built-in in Python. It can store an unordered collection of unique elements of any data type.

For example, the following is a set in Python that store three different unique elements:

>>> s = {1, 'two', 3.0}

The set data structure in Python is similar to the list in the following ways:

  • mutable: we can modify an existing set (but all elements in the set should be immutable).
  • dynamic: we can add or remove elements from a set.

However, unlike the list, the elements in the set are not ordered and indexed. And most importantly it only stores unique elements.

If we try to initialize a set with duplicate values, it automatically removes them and maintains a single copy of each unique element.

>>> s = {1, 2, 1, 3, 3, 4}
>>> print(s)
{1, 2, 3, 4}

How to create a Set in Python?

In Python, we can create a set in two ways:

  1. using the set() contructor
  2. using {} (curly braces)

To create a set using set(), we pass an iterable (such as list, tuple, string, etc) containing the elements, as a parameter.

>>> s = set([1, 2, 3, 4, 5])
>>> print(s)
{1, 2, 3, 4, 5}
>>> type(s)
<class 'set'>

To create a set using {}, we write the elements inside the braces separated by the comma.

>>> s = {1, 'one', 1.0}
>>> print(s)
{1, 'one'}

However, if we do not specify any element inside the {}, Python interpreter will create an empty dictionary instead.

>>> s = {}
>>> type(s)
<class 'dict'>

Therefore, in cases where we want to create an empty set, we should use the set() contructor.

>>> s = set()
>>> type(s)
<class 'set'>

Is Set Mutable in Python?

The set data structure in Python is mutable because we can modify an existing set by adding or removing items, but it can contain only immutable items.

If we try to add or initialize a set with mutable elements (such as a list), the interpreter would throw an error:

>>> s = {1, 2, 3, [4, 5]}
TypeError: unhashable type: 'list'

Although a set itself is mutable, it can only contain immutable elements such as numbers, strings, tuples, etc.

Python Set Operations

There are various operations such as adding or removing items from the set, modifying existing items, combining two sets into one, etc, we can perform on a set data structure in Python. Let’s see each of them one by one.

Access Set Items

Because the elements in the set are not ordered and indexed, we cannot access them using the indexing operator (i.e, []).

So the only way we can access the items of the set is by iterating through its items using an iterator such as for, while, etc.

>>> s = {'Python', 'C++', 'Java'}
>>> for x in s:
...     print(x)
... 
C++
Java
Python

Notice the order of the output does not match with the order of the elements in the set declaration. This is proof that the set does not preserve the order of the elements.

Add New Items

The set has a built-in add() method using which we can easily add new items to an existing set object.

>>> s = {1, 2, 3}
>>> s.add(4)
>>> print(s)
{1, 2, 3, 4}

If we need to add multiple items, we can use another built-in method i.e. update(). Pass the iterable (such as a list) containing the items as a parameter to this method, it will add them all to the existing set object.

>>> s = {'Python', 'C++', 'Java'}
>>> s.update(['C#', 'R'])
>>> print(s)
{'Java', 'Python', 'C#', 'C++', 'R'}

Update Existing Set items

The set cannot store mutable items. Hence, we cannot modify existing items in the set data structure.

Remove Items from Set

Using the discard() and remove() methods, we can easily remove a specific item from the set collection.

The difference between the discard and remove method is that the remove method throws an error when the element that we are trying to delete does not exist in the set, whereas discard does not.

>>> s = {'AK', 'Jon', 'Rahul', 'Rosie'}
>>> s.discard('Mary')
>>> s.discard('AK')
>>> print(s)
{'Jon', 'Rosie', 'Rahul'}
>>> s.remove('Rahul')

>>> print(s)
{'Jon', 'Rosie'}
>>> s.remove('Andrew')
KeyError: 'Andrew'

Alternatively, we can use another built-in method pop() to remove items from a set. Since the set is not ordered, it is not guaranteed which item the pop method will remove.

>>> s = {1, 2, 3, 4}
>>> x = s.pop()
>>> print(s)
{2, 3, 4}
>>> print(x)
1

Difference and Union of Sets

The set data structure in Python has two unique built-in methods: difference and union.

The set.difference(s) returns a set containing all those elements of the current set which are not present in the set s.

>>> x = {1, 2, 8, 5, 6}
>>> y = {2, 8}
>>> x.difference(y)
{1, 5, 6}

Whereas, set.union(s) returns a set that contains all unique elements of the current and s set.

>>> x = {1, 2, 8, 5, 6}
>>> y = {2, 3, 8}
>>> x.union(y)
{1, 2, 3, 5, 6, 8}

Python Set Methods

These are some of the built-in set methods that we can use to perform different tasks on the set object:

  • add(x): add new element (x) to the set.
  • clear(): removes all elements from the set.
  • copy(): returns the copy of set.
  • difference(set2): return the difference of current set with set2 in the form of a set.
  • discard(x): removes x from the set.
  • pop(): removes a random element from the set data structure.
  • update(set2): combine elements of set2 with the current set.
  • union(set2): returns a set which contains all elements from set1 and set2.

Conclusion

A set is a data structure in Python that is used to store unique elements and is unordered, un-indexed, and dynamic.

Since a set can only contain unique elements, we should use it as a collection when we only need to process unique elements in Python.

Leave a Reply