Python Fundamentals – Sets

This note will cover the Python Sets internals and data structure to understand various operations performed on the Sets with sufficient examples. As an expectation, after completing this note, anyone will be able to use Python Sets smoothly.

Python Sets

A set is a data structure that stores an unordered collection of items, and every item or element of the set is unique. Although a user can supply duplicate data elements, it silently drops. The unordered collection of the items mean, data elements can not be accessed via index values, or number and data elements are stored in an arbitrary fashion.

A set is a mutable data structure, which means we can easily add or remove data elements from it and provide a lot more unique and mathematical functionality that does not give any other data structures in Python. It includes functionality such as union, intersection, symmetric difference, etc.

Creating a set

We can create Set using curly bracket {} and initialization with data elements. Otherwise, Python treats it as a dictionary data structure. Suppose we want to initialize it without elements, then we can use the set() function while initializing the set object. We will learn all these concepts with examples in the below sections.

Creating a set without data elements

We can create a set object without data elements in two ways. The first way is just using the curly brackets, open and close, and it is illustrated using the below code.

Example 1:  In this example, we are initializing set object using curly bracket without any elements.

# Set initialization without data elements
init_set = {}
print(type(init_set))
# Output: <class 'dict'>

Example 2:  In this example, we use the set() function, to create a new set object.

# Set initialization without data elements
init_set = set()
print(type(init_set))
# Output: <class 'set'>

As we have seen that, there are two ways by which we can initialize the set data structure with empty elements.

Creating a set with data elements

We can create a set for different homogeneous data types without duplicate elements.

Example 1: In this example, we have illustrated how to create set objects with different data types such as integer, char, and string.

# Set with integer type data type
int_set = {10,9,8,7,6,5,4,3,2}
print(int_set)
# Output: {2, 3, 4, 5, 6, 7, 8, 9, 10}

char_set = {'a','c','b','e','d','a'}
print(char_set)
# Output: {'a', 'b', 'e', 'c', 'd'}

str_set = {'apple','cat','boy','elephant','dog','apple'}
print(str_set)
# Output: {'cat', 'apple', 'dog', 'boy', 'elephant'}

Creating a set using the Python List

Suppose we have a list with multiple data elements and we want unique elements from the list, then we can pass the list object to the set function to get the set object, and it is illustrated using the below example:

lst_obj = [1,1,2,3,4,5,6,7,2,2,3,34,2,1,0,1,0,1]
unique_ele_lst = set(lst_obj)
print(unique_ele_lst)
# Output: {0, 1, 2, 3, 4, 5, 6, 7, 34}

In the above example, we can see that the set construct has removed all the duplicate elements. It is one of the exciting functionality provided by the set data structure.

Adding the elements to a set

As we know, a set is an unordered data structure, which means we can’t access individual elements using an index value. Even if we try to add elements, it gives an error, as shown in the below code.

# Set with integer type data type
int_set = {1,2,10,3,4,5,6,7,8,9}
print(int_set[2])
# Output: TypeError: 'set' object is not subscriptable

So the question comes how we will access elements or add new elements into a set.

Adding an element to a set using add() function

To add an element to a set, we can use the `add` function, and it is illustrated with the help of the below example.

# Set with integer data type
int_set = {1,2,3,4,5,6,7,8,9}
int_set.add(10)
print(int_set)
# Output: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Suppose we try to add multiple elements together using the add function. In that case, it returns an error—the solution to this problem is mentioned in the next section of adding multiple elements.

Adding the multiple elements to a set

It is achieved using the update function. It supports multiple data structures, like lists, tuples, and set as well. It takes all the input elements and merges all of them while silently dropping duplicate elements.

# Set with integer data type
int_set = {1,2,3,4,5,6,7,8,9}
int_set.update([10,11])
print(int_set)
# Output: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}

In the above example, we used a list data type to add multiple elements into a set.

Adding multiple datatypes to a set

We can also add different data objects such as list, set, tuple, etc., as input to the set.

# Set data object 
int_set = {1,2,3,4,5,6,7,8,9}

# Tuple data object
tup_data = (100,101,102)

# List data object
list_data = [10,11,12]

# Adding of different data objects to a set
int_set.update(tup_data,list_data,{13,14,15,16})

print(int_set)
# Output: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 100, 101, 102}

Remove element from a set

We can do this using the discard() function. In the below example, we have an integer collection and want to remove the an integer 1 from the set object.

int_set_obj = {1,2,3,4,5,6,7,8,9}
int_set_obj.discard(1)
print(int_set_obj)
# Output: {2, 3, 4, 5, 6, 7, 8, 9}

There is another function called remove() , and through that function, we can also remove an element from the set.

int_set_obj = {1,2,3,4,5,6,7,8,9}
int_set_obj.discard(1)
print(int_set_obj)
# Output: {2, 3, 4, 5, 6, 7, 8, 9}

There is a difference between the discard() and remove() functions. The discard function ignores if an element is not present in a set object. On the contrary, remove function returns a key error if a requested element is not present in a set object.

Getting a random element from a set

Set construct provides a function called pop , and through this function, we can remove an element randomly from a set object. For example, we have an integer collection, and we want to remove an element from a set.

int_set_obj = {1,2,3,4,5,6,7,8,9}
int_set_obj.pop()
print(int_set_obj)
# Output: {2, 3, 4, 5, 6, 7, 8, 9}

Clearing all the elements from a set

There is a clear function in a Set construct. It is helpful to remove all the elements from the set. For example:

int_set_obj = {1,2,3,4,5,6,7,8,9}
int_set_obj.clear()
print(int_set_obj)
# Output:

Set operations

A set construct provides all the mathematical operations that a mathematical set supports. These operations we will understand with different examples.

Union Operation

It is one of the fundamental operations through which sets can be combined and related to each other. The union of two sets means combining the elements in A or B or both A and B, where A and B are two sets. The operation mainly merges distinct elements from the different sets and creates a new set with distinct elements.

In the below examples, we have taken two set objects. The first contains numbers from 1 to 4, and the second includes numbers from 5 to 7. So using the union operator (|), we will perform union operation.

set1_obj = {1,2,3,4}
set2_obj = {5,6,7}
set_obj = set1_obj | set2_obj
print(set_obj)
# Output: {1, 2, 3, 4, 5, 6, 7}

In this example, we will take a few common elements in both the set objects and check the outcomes.

set1_obj = {1,2,3,4,5,6,7,8,9,10}
set2_obj = {5,6,7,11,12}
set_obj = set1_obj | set2_obj
print(set_obj)
# Output: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

Intersection Operation

We will take a few common elements from both the set objects and check the outcomes in this example.

Example 1:  In this example, we have taken two set objects, each is having few data elements common. Our goal is to find the common elements between the two set objects.

set1_obj = {1,2,3,4,5,6,7,8,9,10}
set2_obj = {5,6,7,11,12}
set_obj = set1_obj & set2_obj
print(set_obj)
# Output: {5, 6, 7}

Example 2: In this example, we have taken three-set objects and try to find the common elements among the set objects.

set1_obj = {5,6,7,8,9,10}
set2_obj = {5,6,7,11,12}
set3_obj = {7}
set_obj = set1_obj & set2_obj & set3_obj
print(set_obj)
# Output: {7}

Set Difference Operation

A set difference can be understood using the following example. Suppose we have two sets, set_obj1 and set_obj2, and if we want to find the elements in set_obj1 but not there in set_obj2. It removes all the common elements and mainly gives the elements only in set_obj1 but not in set obj2. It is represented using the (-) hyphen symbol. To find a set difference, we can use the hyphen or difference() function.

Example 1: In this example, we have taken two set objects, set_obj2 and set_obj2, and try to find the set difference means that, find all the elements are not there in set_obj2 compare to set_obj1.

set1_obj = {5,6,7,8,9,10}
set2_obj = {5,6,7,11,12}
set_obj = set1_obj - set2_obj
print(set_obj)
# Output: {8, 9, 10}

Symmetric Difference Operaton

The symmetric difference is the opposite of intersection operation. In the intersection operation, we get all the elements that are common between two or more sets. Whereas, in the case of symmetric difference, it gives all the elements that are not common among the sets. To find the symmetric difference, we can use the symbol ^ or symmetric_difference() function.

Example 1: In this example, we have taken two set objects, set_obj1 and set_obj2, and try to find the symmetric difference. 

set1_obj = {5,6,7,8,9,10}
set2_obj = {5,6,7,11,12}
set_obj = set1_obj ^ set2_obj
print(set_obj)
# Output: {8, 9, 10, 11, 12}

Example 2: In this example, we have used the symmetric_difference() function to find the difference.

set1_obj = {5,6,7,8,9,10}
set2_obj = {5,6,7,11,12}
set_obj = set1_obj.symmetric_difference(set2_obj)
print(set_obj)
# Output: {8, 9, 10, 11, 12}

Find a subset Operator

It can be easily understood using an example. Suppose we have two sets, set_obj1 and set_obj2. If we want to know whether all the elements of set_obj2 are in set_obj1, then issubset() is very useful. It tells whether a set is a subset of another set or not, and as a result, it returns True or False.

Example 1: In this example, we have two sets, set_obj1 and set_obj2. Our goal is to find whether elements of set_obj2 are in set_obj1 or not.

set1_obj = {5,6,7,8,9,10}
set2_obj = {5,6,7,11,12}
set_obj = set2_obj.issubset(set1_obj)
print(set_obj)
# Output: False

In the above example, set_obj2 is not the subset of set_obj1 as set_obj2 elements are not strictly part of set_obj1.

set1_obj = {5,6,7,8,9,10}
set2_obj = {5,6,7}
set_obj = set2_obj.issubset(set1_obj)
print(set_obj)
# Output: True

Frozen sets

Frozen sets have the characteristics of sets, but it can’t be changed once it is assigned. While tuples are immutable lists, frozen sets are immutable sets. It is created using the function frozenset().
Sets being mutable are unhashable, so they can’t be used as dictionary keys. On the other hand, frozensets are hashable and can be used as keys to a dictionary.

This datatype supports methods like copy(), difference(), intersection(), isdisjoint(), issubset(), issuperset(), symmetric_difference(), and union(). As it is an immutable datatype, so it does not have method that add or remove elements.

This data structure is handy when we want to use it as a key in the dictionary.

References

 96 total views,  1 views today

Scroll to Top
Scroll to Top