Python Sets
Sets
A set is a collection of data which is unordered and unindexed. We represents sets with curly braces { }. In sets duplicate items are not allowed.
E.g. set1 = {'apple', 'mango', 'orange' }
Items in set are unindexed which means you can not access the item individually from the set using its index. But we can loop through the set.
E.g. for i in set1:
print(i)
#output : apple
mango
orange
set() method is used to define the set.
E.g. set2 = set(['one', 'two', 'three'])
print(type(set2)) # output : set
In set we can have any type of data but it should be immutable means the data which does not change. It means that we can not have mutable elements in set like list, dictionary, set etc.
To add the item in set, add() function is used. To add more than one elemet we use update() method.
E.g. set3 = {1, 2, 3, 4}
set3.add(5)
set3.add(6)
print(set3) # output : 1 2 3 4 5 6
set3.update([7, 8, 9])
print(set3) # output : 1 2 3 4 5 6 7 8 9
len() function is used to get the length of set.
To remove the element from set, we have two methods i.e. discard() and remove(). But the difference is that if the removing element does not exist in set then remove() will throw the exception whereas discard() will remain unchanged.
E.g. set4 = {12, 15, 34, 67}
set4.remove(12) # set4 = {15, 34, 67}
set4.discard(34) # set4 = {15, 67}
set4.discard(2) # nothing will happen, set remain unchanged.
set4.remove(2) # exception will appeared.
We can also use pop() method, which removes the last item from the set. Now here is the main information, since sets are unordered, so what pop() function will remove from the set, we never know. It's a surprise so be careful with its use.
clear() function is used to clear the set. It does not delete the set. It makes the set empty. del() function is used to delete the set.
Set Operations :
In mathematics probably you have studied about sets. The operations performed in mathematics set and python set are similar.
1. Union : This operation includes the all items from both the sets. We can use union() method or | operator. Remember that duplicates are not allowed in union also.
E.g. set1 = {1, 5, 2}
set2 = {7, 3, 8}
set3 = set1.union(set2) # set3 = {1, 7, 5, 2, 3, 8} "elements are unordered as we know."
set4 = set1 | set2 # set4 = {1, 2, 5, 3, 8, 7}
2. Intersection : This operations takes the common elements from both sets. We use intersection() method or & operator.
E.g. set1 = {1, 6, 2}
set2 = {2, 3, 6, 8}
set3 = set1.intersection(set2) # set3 = {2, 6}
set4 = set1 & set2 # set4 = {2, 6}
3. Difference : This operations takes the elements from first set which are not in second set. We use - sign to do the difference.
E.g. set1 = {2, 4, 6, 8, 1}
set2 = {3, 4, 6, 0}
set3 = set1 - set2 # set3 = {2, 8, 1}
set4 = set2 - set1 # set4 = {3, 0}
There are other methods also like :
isdisjoint() : Return true if inrersection of both sets is null which means that both sets are disjoint from each other.
symmetric_difference() : This function does the opposite work of intersection method.
Comments
Post a Comment