In the earlier segments, you learnt about lists and tuples, which are ordered sequences. In this segment, you will learn about a data structure 'sets' that is unordered, or, in other words, it doesn’t maintain an order in which elements are inserted. This makes sets unfit for indexing or slicing, but what is their use then? Let’s watch the video below to understand this.
By now, you would have found the answer to our earlier question about why sets are used: They can eliminate duplicates. This feature is necessary while handling massive data where most of it is just redundant and repeating. Let’s take a look at an example to understand this better:
Let’s say you have a huge list that contains student grades:
Grades = ['A', 'A', 'B', 'C', 'D', 'B', 'B', 'C', 'D', 'E', 'C', 'C', 'A', 'B', 'F', 'D', 'C',
'B', 'C', 'A', 'B', 'F', 'B', 'A', 'E', 'B', 'B', 'C', 'D'...]
You want to identify distinct grades allotted to students. Obviously, you cannot check every element of this list; instead, we make use of sets which gets our job done here.
By using the set function on the grades, you would get the distinct grades in the form of a set:
Grades = ['A', 'A', 'B', 'C', 'D', 'B', 'B', 'C', 'D', 'E', 'C', 'C', 'A', 'B', 'F', 'D', 'C, 'B', 'C', 'A', 'B', 'F', 'B', 'A', 'E', 'B, 'B', 'C', 'D'] set(Grades) {'A', 'B', 'C', 'D', 'E', 'F'}
These sets further help you perform all the typical set operations that you learnt in high school.
Imagine you have two sets:
A = {0,2,4,6,8}
B = {1,2,3,4,5}
Set methods
Union represents the total unique elements in both sets.
A.union(B) → {0,1,2,3,4,5,6,8}
Intersection represents the elements common to both sets.
A.intersection(B) → {2,4}
Difference(A-B) represents the elements present in A and not in B.
A.difference(B) → {0,6,8}
Symmetric difference represents the union of the elements A and B minus the intersection of A and B.
A^B → (0,6,8,1,3,5)
The order of elements shown above may not be the same when you actually execute the above operations. It is because sets are unordered, which means they do not store the positional index of an element in the set.
FUN ACTIVITY: Try to decode the below-set operation
(A.union(B)). difference(A.intersection(B))
Instead of using commands, you can also use simple mathematical operators between sets to perform the various operations. Go through the below link to understand how: