Sets in Python

Beginners Code Computer Skills Python

Sets in python

Sets are another form of representing data in a collected form. However, there is no proper indexing to reference data in a set. Therefore we cannot say that a particular data is present at a particular index all the time in a set.

Likewise, lists and Tuples, we can perform various operations on Sets. A set can be thus defined as an unordered collection of data without any proper indexing method.

Defining Sets in Python

A Set can be defined by mentioning the values inside curly braces. Or instead of that, we can also use the inbuilt set() method to define a set. The syntax of both the formats is as follows:

sample_set = {"Tony", "Bruce", "Thor", "Steve"}

By using the inbuilt set() method, we get:

sample_set = set(["Tony", "Bruce", "Thor", "Steve"])

In both cases, it creates a set which has some unordered indices to represent the values.

Accessing Sets in Python

A Set can be accessed like accessing from a normal array. Since the index isn’t present, the order of accessing the set can vary every time we try and access it.

This can be shown by the following code:

sample_set = {"Tony", "Bruce", "Thor", "Steve"}
for i in sample_set:
    print(i)

When we run this code multiple times, we can see that with every time the order of output changes irrespective of the order in which the elements are provided to the set.

Set Operations in Python

We can perform a large number of operations on a Set individually or by combining it with other standard datatypes like Dictionaries, Lists, and Tuples.

Operations on a Set include Union, Intersection, Set Difference, Freezing a set, Addition, Updation, Deletion, etc. We will see the working of all these functions one by one.

Union of Sets

It involves the basic idea: we have two sets and we combine the elements of two sets. This is done by the union() method.

For example:

sample_set_1 = {"Tony", "Bruce", "Thor", "Steve", "Bucky"}
sample_set_2 = {"Peter", "Stephen", "T'chaka", "Vision", "Bucky"}

union_set = sample_set_1.union(sample_set_2)
print(union_set)

Note that, even if we have elements in common, they are only considered once during the union of two sets.

Intersection of Sets

This is used to find the common elements of a set. It is done by using the intersection() method.

For example:

sample_set_1 = {"Tony", "Bruce", "Thor", "Steve", "Bucky"}
sample_set_2 = {"Peter", "Stephen", "T'chaka", "Vision", "Bucky"}

union_set = sample_set_1.intersection(sample_set_2)
print(union_set)

It shows the common element in the two sets.

Set Difference

This is used to subtract the elements from one set and the elements to be subtracted must be present in the second set. It is done by using the difference() method.

For example:

sample_set_1 = {"Tony", "Bruce", "Thor", "Steve", "Bucky"}
sample_set_2 = {"Tony", "Bruce"}

union_set = sample_set_1.difference(sample_set_2)
print(union_set)

Freezing of Sets

This is used to lock a set. It means after this we won’t be able to make any kind of modifications in a set.

For example:

sample_set_1 = {"Tony", "Bruce", "Thor", "Steve", "Bucky"}

union_set = frozenset(sample_set_1)
print(union_set)
union_set.add("Peter")

Here we define another instance of the sample set as a union set. And we declare that set to be a frozen state. When we try to add some new elements, as we have done here, then we will get an error.

Addition of elements

This is used to add new elements to the set. This is done by using the add() method. For example:

sample_set_1 = {"Tony", "Bruce", "Thor", "Steve", "Bucky"}

sample_set_1.add("Peter")
print(sample_set_1)

Updation of elements

This is used to add multiple elements into the set in a single instance. This is done by using the update() method. For example:

sample_set_1 = {"Tony", "Bruce", "Thor", "Steve", "Bucky"}

sample_set_1.update(["Peter","T'chaka", "Sam"])
print(sample_set_1)

Deletion of elements

This is used to delete elements from a set. It is done by using the discard() or remove() or pop() method. For example:

sample_set_1 = {"Tony", "Bruce", "Peter", "T'chaka", "Sam", "Thor", "Steve", "Bucky"}

print(sample_set_1)

#Using remove() method
sample_set_1.remove("T'chaka")
print(sample_set_1)

#Using discard() method
sample_set_1.discard("Sam")
print(sample_set_1)

#Using pop() method
sample_set_1.pop()
print(sample_set_1)

The Difference

discard and remove perform the same operation. The only difference is if the element which is to be removed is not found in the set, then remove() method throws an error while the discard() method doesn’t throw an error.

The pop() method removes the last element present at that instance of the set.