1. Sets

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:

  1. Sets are unordered (sets cannot be indexed or sliced)
  2. Set elements are unique. (Duplicate elements are not allowed)
  3. A set itself may be modified, but the elements contained in the set must be of an immutable type.

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:

Syntax : myset1 = set(["a", "b", "c"]) myset2 = set(("a", "b", "c”)) myset3 = {"a", "b", "c","a"}
In [1]:
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
{'a', 'b', 'c'}
<class 'set'>
<class 'set'>
<class 'set'>

An empty set is falsy in Boolean context:

In [2]:
x = set( )
print(bool(x)) # output will be False

del(x) # deleting the variables created
False

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.

In [3]:
x = {42, 'hello', (1, 2, 3), 3.14159} # tuples are immutable
print(x)
del(x) # deleting the variables created
{3.14159, 42, (1, 2, 3), 'hello'}
Error alert: Elements of sets must be immutable. Hence, any mutable element such as list which is mutable cannot be an element of set. It will generate TypeError
In [4]:
x = {42, 'PJC', [1, 2, 3], 3.14159} # ERROR UNHASHABLE TYPE ‘LIST’
print(x)
del(x) # deleting the variables created
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-fdbfcfb9ec89> in <module>
----> 1 x = {42, 'PJC', [1, 2, 3], 3.14159} # ERROR UNHASHABLE TYPE ‘LIST’
      2 print(x)
      3 del(x) # deleting the variables created

TypeError: unhashable type: 'list'

2. Operating on a Set

Python provides a whole host of operations on set objects that generally mimic the operations that are defined for mathematical sets.

2.1 union

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.

1. Using the method union( )

Syntax: set1.union(set2)

2. Using the | operator

Syntax: set1|set2

The difference between the two approach is, the second approach is not supported when it is used with a set and tuple.
TypeError: unsupported operand type(s) for |: 'set' and 'tuple' will be generated if we try to create union of a set and tuple using the | operator
In [5]:
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
set3 :  {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
set4 :  {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
In [6]:
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
union of set and tuple using the union method :  {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
union of set and tuple using the union method :  {4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50}
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-41d3ab35a6c0> in <module>
      9 
     10 
---> 11 set5=set2|tuple1
     12 print('union of set and tuple using the | operator : ',set5)
     13 

TypeError: unsupported operand type(s) for |: 'set' and 'tuple'
While doing the union one must make sure that the first element must be a set otherwise it will gerenerate error.
AttributeError will be generated if we use the tuple as the first element when doing the union as the tuple object has no attribute called union.
In [7]:
set3=(tuple1).union(set2)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-ce48e4227f8e> in <module>
----> 1 set3=(tuple1).union(set2)

AttributeError: 'tuple' object has no attribute 'union'
In [8]:
set3=(10,20,30).union(set2)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-8ecd33923aeb> in <module>
----> 1 set3=(10,20,30).union(set2)

AttributeError: 'tuple' object has no attribute 'union'

2.2 intersection

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.

1. Using the method intersection( )

Syntax: set1.intersection(set2)

2. Using the & operator

Syntax: set1 & set2
In [9]:
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)
intersection of two sets using the intersection method :  {4, 5}
intersection of set and tuple using the intersection method :  {1, 2, 3, 4}
In [10]:
set5=set1&tuple1
print('intersection of set and tuple using the & operator : ',set4)

del(tuple1, set1, set2, set3, set4, set5) # deleting the variables created
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-31e214428619> in <module>
----> 1 set5=set1&tuple1
      2 print('intersection of set and tuple using the & operator : ',set4)
      3 
      4 del(tuple1, set1, set2, set3, set4, set5) # deleting the variables created

TypeError: unsupported operand type(s) for &: 'set' and 'tuple'

2.3 difference

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

Syntax 1:

set1.difference(set2)

Syntax 2:

set1-set2
In [11]:
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
set1.difference(set2) is  {1, 2, 3}
set1-set2 is  {1, 2, 3}
In [12]:
a = {1, 2, 3, 30, 300}
b = {10, 20, 30, 40}
c = {100, 200, 300, 400}
In [13]:
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
a.difference(b, c) is  {1, 2, 3}
a-b-c is  {1, 2, 3}
When multiple sets are specified, the operation is performed from left to right. In the example above, a - b is computed first, resulting in {1, 2, 3, 300}. Then c is subtracted from that set, leaving {1, 2, 3}:

2.4 symmetric_difference

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:

Syntax 1:

set1.symmetric_difference(set2)

Syntax 2:

set1 ^ set2
In [14]:
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 ^ b is  {1, 50, 5, 10}
result1 ^ c is  {10, 100, 5}
a ^ b ^ c is  {10, 100, 5}
Unlike difference we cannot pass two parameters in symmetric_difference. If we do that then a.symmetric_difference(b, c) will generate __TypeError:__ symmetric_difference() takes exactly one argument (2 given)
In [15]:
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
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-9165c0210572> in <module>
      3 c = {1, 50, 100}
      4 
----> 5 result1=a.symmetric_difference(b, c)
      6 print('a.symmetric_difference(b, c) is ', result1)
      7 

TypeError: symmetric_difference() takes exactly one argument (2 given)
In [16]:
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
a.symmetric_difference(b) is  {1, 50, 5, 10}
result1.symmetric_difference(c is  {10, 100, 5}

2.5 isdisjoints

Given two sets, set1 and set2 the set1.isdisjoint(set2) returns True if set1 and set2 have no elements in common

Syntax:

set1.isdisjoint(set2)
In [17]:
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))
set1.isdisjoint(set2) is  True
set1.isdisjoint(set3) is  False