Python Sets


Introduction to Python Set

Python Set is a collection of unique elements that are unordered. Items in the set do not have any index positions.

We use curly brackets ‘{}’ to define the set. By default, these curly brackets are used for the dictionary but if we have a comma-separated list of elements inside these curly brackets ‘{}’, then it is considered as the set. We can create a set using the set() function as well.

In [1]:
first_set = {1,2,3,4,4,4,4,5,5,'string',24.8}

Note there are no repeated elements.

In [2]:
print(first_set)
{1, 2, 3, 4, 5, 'string', 24.8}

Characteristics of a Set

Let us look at the characteristics of the Set one by one:

CharacteristicMeaning
UnchangeableThis means that we cannot change the items stored within the set. We can add or delete items to set.
No DuplicatesDuplicate items are not allowed in the set.
UnorderedThe items in a set do not have a defined order.
HeterogeneousSets can store elements of various data types, but only immutable values.
Characteristics of a Python Set

Creating an Empty Set

We use curly brackets to enclose the set, but an empty set cannot be created using curly brackets because, by default, it will create a dictionary. A curly bracket with one element is a set.

To create an empty set, we can use the set() function.

Curly brackets ‘{}’ by default is considered as a dictionary.

In [1]:
emptyset = {}
print(type(emptyset))
<class 'dict'>

If curly brackets ‘{}’ has some value, then it is a set.

In [2]:
emptyset = {1}
print(type(emptyset))
<class 'set'>

set() constructor to create empty set.

In [3]:
emptyset = set()
print(type(emptyset))
<class 'set'>

Handling Duplicates

A set cannot have duplicate items, if we try to create a set with duplicates or even try to insert duplicates in the set, then duplicates will be automatically removed.

Variable ‘a’ is assigned with many duplicate values, but on printing a, there is no duplicate.

In [1]:
numbers_set = {1,2,3,4,4,4,4,5,5,8,9}
print(numbers_set)
{1, 2, 3, 4, 5, 8, 9}

Trying to add new value to ‘a’, it works.

In [2]:
numbers_set.add(10)
print(numbers_set)
{1, 2, 3, 4, 5, 8, 9, 10}

Trying to add value that is already available in ‘a’, it does not get added.

In [3]:
numbers_set.add(3)
print(numbers_set)
{1, 2, 3, 4, 5, 8, 9, 10}

Sets are Unchangeable

As discussed above, items in the set do not have index positions, so there is no point in changing any value in the set using its index position.

In [1]:
numbers_set = {1,2,3,4,5,8,9}
print(numbers_set)
{1, 2, 3, 4, 5, 8, 9}

We get TypeError saying “‘set’ object does not support item assignment”.

In [2]:
numbers_set[1] = 7
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-0f42311ab66a> in <module>
----> 1 numbers_set[1] = 7

TypeError: 'set' object does not support item assignment

Sets are Unordered

Items in the set do not follow any specific order. Every time, the content of the set may appear in different order. This is the reason that items in the set cannot be referred by index locations.

The order in which we assigned the items and the order in which we get items is different. Next time we may get some different order.

In [1]:
numbers_set = {91,36,26,78,14,89,67}
print(numbers_set)
{67, 36, 78, 14, 89, 26, 91}

type() function

At any stage in our program, we may need to check whether the variable is a set or not, we can use the type() function for this purpose. If the type of the variable is set, then the type() function will return the ‘set’ keyword.

In [1]:
a = {1,2,3,'x',True}
print(type(a))
<class 'set'>

Access Set Items

There is no way we can refer to the elements of the set using the index. But it is possible to loop through the elements of the set using the for loop.

As set does not have nay order, so output does not have any order.

In [1]:
my_set = {'a', 'b', 'c', 'd'}

for i in my_set:
    print(i)
d
b
c
a

Check if two sets are equal

The equal to (==) is used to check if two sets have the same values. Note: As the sets are unordered, so it is not necessary that elements in both sets have the same order. If both sets have the same elements in any order, then too they are equal.

Both sets are equal irrespective of the order of elements.

In [1]:
my_set1 = {1, 2, 3, 4}
my_set2 = {3, 2, 1, 4}

print (my_set1 == my_set2)
True

Element 4 is missing in my_set2.

In [2]:
my_set1 = {1, 2, 3, 4}
my_set2 = {3, 2, 1}

print (my_set1 == my_set2)
False

Disjoint Sets

Python has an inbuilt method, isdisjoint() to check whether two sets have anything in common or not. It returns true if there are no elements in common, and returns False if there are one or more elements in common.

In [1]:
my_set1 = {1, 2, 3, 4}
my_set2 = {5, 6, 7, 8}

print (my_set1.isdisjoint(my_set2))
True
In [2]:
my_set1 = {1, 2, 3, 4}
my_set2 = {4, 6, 7, 8}

print (my_set1.isdisjoint(my_set2))
False

Copy a set

There are three ways to copy the set:

  1. Using the set() constructor.
  2. Using the copy() method.
  3. Using the = operator.

1. Using the assignment operator(=): It creates the reference to the original set. If we make a change in the original set, it will be reflected in the copied set

Change of my_set1 got reflected in my_set2.

In [1]:
my_set1 = {1, 2, 3, 4}
my_set2 = my_set1

my_set1.add(5)

print (my_set2)
{1, 2, 3, 4, 5}

2. Using the copy() method: It will create the new set. Making changes in the original set will not be reflected in the new set.

Change of my_set1 is not reflecting in my_set2.

In [1]:
my_set1 = {1, 2, 3, 4}
my_set2 = my_set1.copy()

my_set1.add(5)

print (my_set2)
{1, 2, 3, 4}

3. Using the set() constructor: Again, it will create a new copy of set.

Change of my_set1 is not reflecting in my_set2.

In [1]:
my_set1 = {1, 2, 3, 4}
my_set2 = set(my_set1)

my_set1.add(5)

print (my_set2)

Membership Test

To check whether the item is present in the set or not, we can use the membership operator in and not in.

In [1]:
my_set1 = {1, 2, 3, 4}

print(1 in my_set1)
True
In [2]:
my_set1 = {1, 2, 3, 4}

print(1 not in my_set1)
False

Number of items in set

The len() function can be used to check the number of items in the set.

In [1]:
my_set1 = {1, 2, 3, 4}

print(len(my_set1))
4

Frozen Sets

In Python, the forzenset() is an in-built function that is used to create an immutable version of a set. It can take any iterable like list, tuple, as an input and returns a frozen set. In the set, we can add or remove the items once it is created, but in the frozen set we cannot do anything. In the code below, please observe that we cannot add an item to frozen set.

List to frozen set.

In [1]:
my_list = [1, 2, 3, 4]

my_frozen_set = frozenset(my_list)

print(my_frozen_set)
frozenset({1, 2, 3, 4})

Try to add element in frozen set.

In [2]:
my_list = [1, 2, 3, 4]

my_frozen_set = frozenset(my_list)

my_frozen_set.add(2)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-d90ab86569a0> in <module>
      3 my_frozen_set = frozenset(my_list)
      4 
----> 5 my_frozen_set.add(2)

AttributeError: 'frozenset' object has no attribute 'add'

Uses:

  1. When we need a strict read-only set, that should not be modified at all.
  2. In situations where we need a set-like object that can be used as a key in a dictionary or as an element in another set.

Operations :

  1. We can perform: union(), intersection(), difference(), and symmetric_difference()
  2. We cannot perform because they are immutable: intersection_update(), difference_update(), and symmetric_difference_update()

References

  1. Python Sets
  2. Unordered collections of unique elements