A Set is an unordered collection data type that is iterable, mutable and has no duplicate elements.
Python’s built-in set type has the following characteristics:
Python’s set class represents the mathematical notion of a set. The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set. This is based on a data structure known as a hash table. Since sets are unordered, we cannot access items using indexes like we do in lists. A set can be defined in three ways as shown below:
myset1 = set(["a", "b", "c"])
myset2 = set(("a", "b", "c"))
myset3 = {"a", "b", "c","a"}
print(myset1)
print(type(myset1))
print(type(myset2))
print(type(myset3))
del(myset1, myset2, myset3) # deleting the variables created
An empty set is falsy in Boolean context:
x = set( )
print(bool(x)) # output will be False
del(x) # deleting the variables created
The elements in a set can be objects of different types except mutable elements i.e., lists and dictionariesn cannot be elements of sets as they are mutable but tuple can be an elment in sets as they are immutable.
x = {42, 'hello', (1, 2, 3), 3.14159} # tuples are immutable
print(x)
del(x) # deleting the variables created
x = {42, 'PJC', [1, 2, 3], 3.14159} # ERROR UNHASHABLE TYPE ‘LIST’
print(x)
del(x) # deleting the variables created
Given two sets, x1 and x2, the union of x1 and x2 is a set consisting of all elements in either set. The union of sets can be achieved in two ways.
set1={1,2,3,4,5}
set2={4,5,6,7,8,9,10}
set3=set1.union(set2)
print('set3 : ',set3)
set4=set1|set2
print('set4 : ',set4)
del(set1, set2, set3, set4) # deleting the variables created
tuple1=(1,2,3,4)
set2={4,5,6,7,8,9,10}
set3=set2.union(tuple1)
print('union of set and tuple using the union method : ',set3)
set4=set2.union((20,30,40,50))
print('union of set and tuple using the union method : ',set4)
set5=set2|tuple1
print('union of set and tuple using the | operator : ',set5)
del(tuple1, set2, set3, set4, set5) # deleting the variables created
set3=(tuple1).union(set2)
set3=(10,20,30).union(set2)
If there are two sets, then the intersection of two sets will result in aset having only the common elements which are present in both the sets.
The intersection of sets can be achieved in two ways.
tuple1=(1,2,3,4)
set1={1,2,3,4,5}
set2={4,5,6,7,8,9,10}
set3=set1.intersection(set2)
print('intersection of two sets using the intersection method : ',set3)
set4=set1.intersection(tuple1)
print('intersection of set and tuple using the intersection method : ',set4)
set5=set1&tuple1
print('intersection of set and tuple using the & operator : ',set4)
del(tuple1, set1, set2, set3, set4, set5) # deleting the variables created
Given two sets set1 and set2, the set1.difference(set2) or set1 - set2 return the set of all elements that are in set1 but not in set2
set1={1,2,3,4,5}
set2={4,5,6,7,8,9,10}
set3=set1.difference(set2)
print('set1.difference(set2) is ', set3)
set4=set1-set2
print('set1-set2 is ', set4)
del(set1, set2, set3, set4) # deleting the variables created
a = {1, 2, 3, 30, 300}
b = {10, 20, 30, 40}
c = {100, 200, 300, 400}
result1=a.difference(b, c)
print('a.difference(b, c) is ', result1)
result2=a-b-c
print('a-b-c is ', result2)
del(a, b, c, result1, result2) # deleting the variables created
Given two sets, set1 and set2, the symmetric_difference is defined as set1.symmetric_difference(set2) or set1 ^ set2 return the set of all elements in either x1 or x2, but not both:
a = {1, 2, 3, 4, 5}
b = {10, 2, 3, 4, 50}
c = {1, 50, 100}
result1=a^b
print('a ^ b is ',result1)
result2= result1 ^ c
print('result1 ^ c is ', result2)
result3=a ^ b ^ c
print('a ^ b ^ c is ', result3)
del(a, b, c, result1, result2, result3) # deleting the variables created
a = {1, 2, 3, 4, 5}
b = {10, 2, 3, 4, 50}
c = {1, 50, 100}
result1=a.symmetric_difference(b, c)
print('a.symmetric_difference(b, c) is ', result1)
del(result1) # deleting the variables created
a = {1, 2, 3, 4, 5}
b = {10, 2, 3, 4, 50}
result1=a.symmetric_difference(b)
print('a.symmetric_difference(b) is ',result1)
c = {1, 50, 100}
result2= result1.symmetric_difference(c)
print('result1.symmetric_difference(c is ', result2)
del(a, b, c, result1, result2) # deleting the variables created
Given two sets, set1 and set2 the set1.isdisjoint(set2) returns True if set1 and set2 have no elements in common
set1={1,2,3,4}
set2={5,6,7,8}
set3={3,4,5,6}
print('set1.isdisjoint(set2) is ', set1.isdisjoint(set2))
print('set1.isdisjoint(set3) is ', set1.isdisjoint(set3))