Operations and Usage of Sets - Python

• Set is a built in data-type(data-structure) in python.
• It only stores unique elements.
• It do not contains duplicate elements.
• We can iterate over sets.

What is set ?

In mathematics, a set is a well-defined collection of distinct objects, considered as a single object in its own right.

How to create a set in python?

`# case1: empty set with built-in keywords = set()print(s)# Output: set([])print(type(s))# Output: set# case2: set with initial datas = {1, 'a', '@', 'batta', 2.22}print(s)# Output: set([2.22, 'batta', 1, 'a', '@'])print(type(s))# Output: set`

How to add an element to a set in python ?

`s = {1,2,3,4,5,6,'a'}print(s)# Output: set(['a', 1, 2, 3, 4, 5, 6])s.add(100)print(s)# Output: set(['a', 1, 2, 3, 4, 5, 6, 100])`

How to remove an element from set in python ?

`s = {1,2,3,4,5,6,'a'}r = s.remove(5)print(s)# Output: set(['a', 1, 2, 3, 4, 6])print(r)# Output: Nones = {1,2,3,4,5,6,'a'}r = s.discard(5)print(s)# Output: set(['a', 1, 2, 3, 4, 6])print(r)# Output: None`

How to remove and get last element from set in python ?

`s = {1,2,3,4,5,6,'a'}r = s.pop()print(s)# Output: set([1, 2, 3, 4, 5, 6])print(r)# Output: 'a'`

How to clear or empty the set in python?

`s = {'a', 1,2,3,4,5,6}s.clear()print(s)# Output: set([])`

How to copy set1 to set2 ?

`s1 = {1,2,3,4, 'a', 'hello', 1.255}# id - it is a built-in function it returns the memory location of python object.s2 = s1.copy()print(id(s1))# Output: 139864877446360print(id(s2))# Output: 139864877446128# if we do not use 'copy' s3 = {1,2,3,4}s4 = s3print(id(s3))# Output: 139864878456528print(id(s4))# Output: 139864878456528`

How to find difference of two sets in python ?

`s1 = {1,2,3,4,5,6,7,8}s2 = {4,5,6,9, 10, 11, 12, 13, 14}difference = s1.difference(s2)print(difference)# Output: set([8, 1, 2, 3, 7]) # shortcut methoddifference = s1 - s2print(difference)# Output: set([8, 1, 2, 3, 7]) # It do not modify the initial data in s1, s2print(s1)# Output: set([1, 2, 3, 4, 5, 6, 7, 8])print(s2)# Output: set([4, 5, 6, 9, 10, 11, 12, 13, 14])`

How to update the difference of set1, set2 into set1 ?

`s1 = {1,2,3,4,5,6,7,8}s2 = {4,5,6,9, 10, 11, 12, 13, 14}difference = s1.difference_update(s2)print(difference)# Output: Noneprint(s1)# Output: set([1, 2, 3, 7, 8])print(s2)# Output: set([4, 5, 6, 9, 10, 11, 12, 13, 14])`

How to find the common elements in set1, set2 in python?

`s1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}s2 = {4, 5, 6, 9, 10, 11, 12, 13, 14}intersection = s1.intersection(s2)print(intersection)# Output: set([4, 5, 6, 9, 10, 11])`

How to check whether set1 and set2 contains common elements or not ?

`# find if sets disjoint or notset1 = set([1, 2.55, 3, "a"])set2 = set(["hello", "world", "batta"])is_disjoint = set1.isdisjoint(set2)print(is_disjoint)# Output: True# Because there are no common elementsset1 = set([1, 2.55, 3, "a"])set2 = set(["hello", 1, "a"])is_disjoint = set1.isdisjoint(set2)print(is_disjoint)# Output: False# common elements are 1, "a"`

How to check if given set s1 is subset of other set s2 or not ?

`# case1set1 = {0, 1, 2, 3, "abcd", 5, 6, 7, 8, 9} # supersetset2 = {1, 3, 5} # subsetresult = set2.issubset(set1)print(result)# Output: True# case2set1 = {0, 1, 2, 3, "abcd", 5, 6, 7, 8, 9} # supersetset2 = {1, 3, 5} # subsetresult = set1.issubset(set2)print(result)# Output: False# case3set1 = {0, 1, 2, 3, "abcd", 5, 6, 7, 8, 9}set2 = {1, 3, 5, "extra element"}result = set2.issubset(set1)print(result)# Output: False`

How to check if given set s1 is superset of other set s2 or not ?

`# case1set1 = {0, 1, 2, 3, "abcd", 5, 6, 7, 8, 9} # supersetset2 = {1, 3, 5} # subsetresult = set1.issuperset(set2)print(result)# Output: True# case2set1 = {0, 1, 2, 3, "abcd", 5, 6, 7, 8, 9} # supersetset2 = {1, 3, 5} # subsetresult = set2.issuperset(set1)print(result)# Output: False# case3set1 = {0, 1, 2, 3, "abcd", 5, 6, 7, 8, 9}set2 = {1, 3, 5, "extra element"}result = set1.issuperset(set2)print(result)# Output: False`

How to combine(union) set1 and set2 ?

`set1 = {0, 1, 2, 3, 5, 6, 7, 8, 9, 'abcd'}set2 = {1, 3, 5, 'extra element'}result = set1.union(set2) # it will not modify the original sets & returns union of two sets.print(result)# Output: {0, 1, 2, 3, 5, 6, 7, 8, 9, 'abcd', 'extra element'}# check if original sets changed or notprint(set1)# Output: {0, 1, 2, 3, 5, 6, 7, 8, 9, 'abcd'}prnt(set2)# Output: {1, 3, 5, 'extra element'}`

How to update set1 with other set set2 ?

`set1 = {0, 1, 2, 3, 5, 6, 7, 8, 9, 'abcd'}set2 = {1, 3, 5, 'extra element'}result = set1.update(set2) # it will update the operating set & returns None.print(result)# Output: Noneprint(set1)# Output: {'abcd', 1, 2, 3, 5, 6, 7, 8, 9, 'extra element', 0}print(set2)# Output: {1, 3, 5, 'extra element'} `

How to find symmetric difference of two sets ?

symmetric difference is known as the disjunctive union, of two sets is the set of elements which are in either of the sets and not in their intersection.
`set1 = {0, 1, 2, 3, 5, 6, 7, 8, 9, 'abcd'}set2 = {1, 3, 5, 'extra element'}result = set1.symmetric_difference(set2) # It will not change the original sets.# it removes common elements in both sets and returns the union of remaining elements.print(result)# Output: {0, 2, 6, 7, 8, 9, 'abcd', 'extra element'}# print original setsprint(set1)# Output: {0, 1, 2, 3, 5, 6, 7, 8, 9, 'abcd'}print(set2)# Output: {1, 3, 5, 'extra element'}`

How to find symmetric difference of two sets and update the result in same set?

`set1 = {0, 1, 2, 3, 5, 6, 7, 8, 9, 'abcd'}set2 = {1, 3, 5, 'extra element'}result = set1.symmetric_difference_update(set2)# it removes common elements in both sets and updates set1 with union of remaining elements.print(result)# Output: None# print original setsprint(set1)# Output: {0, 2, 6, 7, 8, 9, 'abcd', 'extra element'}print(set2)# Output: {1, 3, 5, 'extra element'}`
Note: You can pass any iterable data to set methods.