Sets are another collection datatype which contains unordered elements. These elements cannot be indexed like lists or tuples. Sets are mutable. So, we can add or remove elements.
We can perform set theory operations like we studied in mathematics in high school: union, intersection, symmetrical difference, etc.
Creating Sets
We can creates a set using curly braces ({}). We could also use set() function to create a set from any iterable. It is an object of type set.
For creating empty set, we have to use set() function. We cannot use {} for creating empty set. This is because the later symbol is reserved for creating empty dictionary. We will learn about this data structure in the next lesson.
1# Empty sets
2empty_set = set()
3type(empty_set) # set
4
5numbers = {1, 2, 3, 4, 5}
6print(numbers) # {1, 2, 3, 4, 5}
7
8numbers = set(range(5))
9print(numbers) # {0, 1, 2, 3, 4}
Sets are unordered collection. It means they cannot be indexed like lists or tuple. If we try to get an item from specific index, we get TypeError.
1numbers = {1, 2, 3, 4, 5}
2print(numbers[1])
Output:
TypeError: 'set' object is not subscriptable
We can add an element to existing set using add method and remove an element using remove method.
1colors = {"black", "white", "blue"}
2colors.add("red")
3print(colors)
4colors.remove("black")
5print(colors)
Output:
{'red', 'white', 'black', 'blue'}
{'white', 'blue', 'red'}
One of the important properties of sets is that it cannot have duplicates. If I try to add an element twice, it will simply discard that element and will not add to existing set.
Example:
1colors = {"black", "white", "blue"}
2print(colors)
3colors.add("red") # correctly adds this color
4print(colors)
5colors.add("white") # does not add 'white' second time
6print(colors)
Output:
{'white', 'black', 'blue'}
{'red', 'white', 'black', 'blue'}
{'red', 'white', 'black', 'blue'}
Once we have sets, we can perform different set theory operations on them.
We can find union of two sets. This will simply merge the elements of two sets. We can also use | symbol for this operation.
1set1 = {1, 2, 3, 4, 5}
2set2 = {4, 5, 6, 7}
3print(set1.union(set2)) # {1, 2, 3, 4, 5, 6, 7}
4print(set1 | set2) # {1, 2, 3, 4, 5, 6, 7}
We can find intersecting elements using intersection operation. This returns only the common elements between two sets.
1set1 = {1, 2, 3, 4, 5}
2set2 = {4, 5, 6, 7}
3print(set1.intersection(set2)) # {4, 5}
4print(set1 & set2) # {4, 5}
We can also find the difference of two sets. This will return the difference of two sets. It will return elements which are in first set, but not in the second one.
1set1 = {1, 2, 3, 4, 5}
2set2 = {4, 5, 6, 7}
3print(set1.difference(set2)) # {1, 2, 3}
4print(set1 - set2) # {1, 2, 3}
We can find symmetrical difference of two sets. This will return all the elements from both sets which are present in one of the sets but not both.
1set1 = {1, 2, 3, 4, 5}
2set2 = {4, 5, 6, 7}
3print(set1.symmetric_difference(set2)) # {1, 2, 3, 6, 7}
4print(set1 ^ set2) # {1, 2, 3, 6, 7}
We can find out if two sets are disjoint sets. These are sets which have no matching elements between them.
1set1 = {1, 2, 3}
2set2 = {4, 5, 6}
3print(set1.isdisjoint(set2)) # True
We can validate if a set is subset of another set. This will return True if all elements are present in the given set. Similarly, we can use issuperset method to check the reverse of issubset method.
1set1 = {1, 2, 3, 4, 5}
2set2 = {4, 5, 6, 7}
3set3 = {4, 5}
4print(set1.issubset(set2)) # False
5print(set3.issubset(set2)) # True
6print(set1.issuperset(set2)) # False
We can clear all elements of a set using clear() method. It will remove all elements from the set.
1numbers = {1, 2, 3}
2print(numbers) # {1, 2, 3}
3numbers.clear()
4print(numbers) # set()


Comments