Python Sets: Introduction

A Python set is an unordered collection of comma separated elements inside curly braces '{ }'. Python set itself is mutable allowing addition and removal of items but the elements are immutable which forbids any change in existing items.

Unlike tuples or lists, Python sets don’t allow multiple occurrences of the same element. Hence, each element in Python set is unique.

There are two types of built-in set types:

set: Python set type is mutable

frozen set: Python frozen set type is immutable

How to create Python sets?

Creating set in Python is as easy as placing unique comma separated items in curly braces '{ }' or it can also be created by using built-in function set( ).

We can’t create empty sets by using only curly braces in Python sets though, we need to use built-in function set( ) to create an empty set.

We can use built-in function set( ) to create a set from a string. When we pass a string in the function set( ), the string is broken into its singular characters and the characters form a new set. Here is an example.

What happens when we try to create Python sets from lists or tuples containing duplicate elements?

Well, we know that Python set is the unordered collection of unique items. So, when we try to create a set from list or tuple containing duplicate items as well, the function set( ) automatically excludes the duplicate items preventing from their multiple occurrences.

How to remove items from a Python set?

While the existing items in a Python set can’t be changed, we can add or remove items. There are certain methods to remove items from a Python set. Each method is illustrated with examples below.

pop( )

Like in lists and tuples, pop( ) is used to remove an item from a Python set. In lists or tuples or dictionary we can remove the desired item specifying in the slicing and indexing operator.

Since slicing and indexing operator is not allowed in sets, an arbitrary item is removed and returned. Here is the example.

>>> py_set = set([1,2,3])
>>> py_set.pop()
1
>>> print (py_set)
{2,3}

remove( )

Using remove( ) function, we can remove any existing item in the Python set by designating and supplying the item as the function argument. It will throw and error if the item doesn’t exist in the set.

Python mathematical set operations

Different mathematical operations like union, intersection, and difference can be carried out using Python sets.

Set Union

As the definition goes in the set theory, Set union is defined as the set of collection of all the elements from corresponding sets. It is one of the fundamental operation through which different sets can be related.

In mathematics, union is represented by U.

In Python, union can be achieved by either using | operator or by using built-in function union( ).

As seen in above example, the elements common to both sets 3,4, and 5 are excluded.

Subset and Superset

Any set X is called the subset of Y, if and only if all the elements of X exist in Y. Here X is the subset of Y and Y is the superset of X as Y contains all the elements of X.

In Python, we can check whether or not a set is subset of another by using <= operator or by using built-in function issubset( ). Also we can check superset by using >= operator or using built-in function issuperset( ).

What is a Python frozen set?

Frozen set is similar to set except in mutability. Python set is mutable itself, however, the elements in the set are immutable. In the frozen set, along with elements frozen set itself is immutable. Hence, We cannot add or remove items from frozen sets.

Frozen sets can be used as keys in the dictionary as they are immutable and hashable.

Frozen sets are created using built-in function frozenset( ).

>>> py_frozenset = frozenset([1,2,3,4,5])

Python Set Methods

Here is the tabulated list if Python set methods used for different kind of operations in sets.

Python set methods with description

set.add( )
Adds an element to the set.

set.clear( )
Remove all the items from the set.

set.copy( )
Returns a shallow copy of the set. Used for copying.

set.difference( )
Returns the difference of two or more sets.

set.discard( )
Removes an element from a set if it exist in the set.

set.intersection( )
Returns the set of common elements between sets i.e intersection.

set.disjoint( )
Returns true if the sets don’t have elements in common i.e null intersection.

set.issubset( )
Returns true if another set contains all the elements of this set.

set.issuperset( )
Returns true if this set contains all the elements of another set.

set.pop( )
Returns and removes an arbitrary item from the set. Raises error if the set is empty.

set.remove( x)
Removes the element x from the set. If x doesn’t exist in the set, it raises error.

set.symmetric_difference( )
Performs symmetric difference and returns the set of all the members of the sets involved excluding the common elements.

set.union( x)
Performs and the returns the union of the sets as a set.