Set is a Data Structure in Python with an unordered and unindexed collection of elements. Every element in a Set is always unique. The Set Data Structure does not allow any duplication of elements.
The Set is similar to the list Data Structure when it comes to mutability. Both Sets and Lists are mutable. This means that elements can be added or removed from these ‘Sets’ data structures that are created.
Sets can be used to perform mathematical set operations like union, intersection, symmetric difference, etc. Hence, they find their usage for mathematical computations making it an overall important data structure for performing specific operations.
Now that we have a basic understanding of sets, let us proceed to understand some of the basic operations and functionalities of Sets. We will cover most of the essential concepts you need to know to master the Set Data Structure.
There are two main methods of creating a set. One method is to use the set function that is available in Python. The other method is to use the curly braces ‘{}’ and type the list of various elements.
The sets that are created cannot be indexed because they are unordered elements. If any element is repeated in the set, it is not accounted for and will be disregarded. The elements in a set are always arranged in ascending order.
Let us explore this topic with a few code blocks to gain a better understanding of the topic.
Output:
set([1, 2, 3])
<type 'set'>
The code block described above provides us with one of the methods of creating a Set of elements. We can notice that we have made use of the curly braces to create a list. The type function is used to indicate that the variable ‘s’ defined by the user now belongs to the class ‘set’.
In the next code block, we will look at the other method to create a set.
Output:
{1, 2, 3, 4, 8}
In the second method of creating a set, we can notice that I tried to implement a set and provided a repetition of the element 2 and arranged the order of elements in an unorganized manner. However, the set function removed any duplicate elements and arranged all of the elements in an organized ascending order.
Note: Trying to index an element in a set is not possible because it is unordered and unindexed. Trying to do so will result in the following error.
TypeError: 'set' object does not support indexing
We have already discussed that the set data structure is mutable. Hence, it is possible to add additional elements to the set. There are two main methods of performing this operation. We can acknowledge this with the add() and the update() functions provided in the Set data structure.
The add() method adds an element to the set. If the element is already present in the set, then the following function will skip the adding operation and no further step is required.
Let us look at the operation of the add() function in the set data structure.
Output:
{1, 2, 3}
In the above code block, we can notice that the initial defined set had the elements 1 and 2. With the use of the add() function, we were able to add an additional element 3 to the existing set.
In the next code block, we will look at the update() function which can be used to add multiple elements to the set. If there are any repetitions, then those elements are ignored.
Output:
{1, 2, 3, 4, 5, 6}
The update() function as described in the above code block can add multiple elements to the particular set. Similar to the add() function, any repetition of elements is disregarded and all the elements are arranged in the ascending order.
We already determined multiple times that a set is mutable and hence even removing elements from a set is possible. The removing of elements from a set can be done in mainly three ways, namely, remove(), discard(), and pop. Let us look at the code block below and the respective output received from the following lines of codes to determine how exactly these functions work.
Output:
{1, 2, 3, 4}
{1, 2, 3, 4}
{2, 3, 4, 5}
The remove function is used to remove a pre-existing element from the set. In case the element you are trying to remove does not exist in the set, you will receive the a key error.
To combat this issue, we have the discard function. If the element to be present in the set is present, then the element is removed. If the element is absent in the set, then the operation proceeds normally without any errors.
The pop() function is used to randomly remove an element from the set. The pop() method assigned to a variable results in a random value, and you can obtain then the pop() method returns the removed element to the variable. Refer to the below example to understand this statement more clearly.
x = s.pop()
Here, 'x' would store the value of 1.
The set data structure allows the user to perform a variety of unique mathematical computations and operations. These operations can easily be understood by viewers who have read concepts of sets and Venn diagrams in their high school.
Let us analyze some of the operations that can be performed with the various functions available in the set data structure.
1. Union
The union of a collection of sets is the set of all elements in the collection. In Python sets, we can use the ‘|’ operator or the union function to perform this operation. This operation can be computed as follows:
Output:
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5}
2. Intersection:
The intersection function is used to obtain similar elements from two particular sets. In Python sets, we can use the ‘&’ operator or the intersection function to perform this operation. This operation can be computed as follows:
Output:
{3}
{3}
3. Set Difference and Other Operations:
The set difference is the set of elements that are only in set1 but not in set2. This operation can be performed as follows:
Output:
{1, 2}
{1, 2}
The symmetric difference is set of elements in both set1 and set2 except those that are common in both. This operation can be performed as follows:
Output:
{1, 2, 4, 5}
{1, 2, 4, 5}
With this, we have covered most of the essential concepts of sets and can proceed to the other data structures.