set is an abstract data type that represents an unordered collection of unique elements.

Sets just like lists are mutable meaning that operations such as insertion and deletion can be performed on them in-place and without  creating a new set. 

In Python, sets are represented using the  set type/class. This class defines a number of methods to support various operations on sets. The operations can be sub-divided into: 

  1. Elementary operations such as insertion, deletion, etc.
  2. Set operations such as union, intersection, difference, etc .

Methods for Elementary operations

Set, like most data structures, supports basic operations such as Insertion and deletion. This is achieved through several methods defined in the set class.

add()

The add() method is used to add a single element to the set. It takes one parameter, which is the element to be added

Syntax:
myset.add(x)
x The element to be added to the set.

Sets can only contain unique elements thus the operation will have no effect if the element already exists.

ExampleEdit & Run
myset = {1, 2, 3}

#add some elements
myset.add(4)
myset.add(5)

#this has no effect because the elements exists.
myset.add(2)
myset.add(1)

print(myset)
Output:
{1, 2, 3, 4, 5} [Finished in 0.011214813683182001s]

clear() 

The clear() method takes no argument, it removes all elements present in a set effectively turning it to an empty set.

Syntax:
myset.clear()
ExampleEdit & Run
#a non-empty set
myset = {1, 2, 3, 4}

#remove all elements
myset.clear()
print(myset)
Output:
set() [Finished in 0.01028103893622756s]

copy()

The copy() method is used to create a shallow copy of the set.This means that  the method returns a new set object that contains all the elements of the original set but will not be affected if any changes are made to the original set.

The method takes does not take any arguments.

Syntax:
myset.copy()

The method returns a new set which is a shallow copy of the original set.

ExampleEdit & Run
myset = {3, 5, 7}

copied = myset.copy()
print(copied)
Output:
{3, 5, 7} [Finished in 0.009631182998418808s]

discard()

The discard() method removes an element from the set if  it is present. It does not raise an exception if  the element is not present.

Syntax:
myset.discard(x)
x The element to be removed.
ExampleEdit & Run
myset = {1, 3, 5, 6, 7, 8, 9}

#remove an element
myset.discard(6)
myset.discard(8)

print(myset)

#no exception is raised if the element does not exist
myset.discard(10)
Output:
{1, 3, 5, 7, 9} [Finished in 0.01021553436294198s]

pop() 

The pop() method removes and returns an arbitrary/random element from the set. It raises a KeyError if the set is empty.

Syntax:
myset.pop()

The method takes no arguments, it returns the removed arbitrary element.

ExampleEdit & Run
myset = {0, 2, 4, 6, 8, 10}

#remove and return an arbitrary element
print(myset.pop())
print(myset.pop())
print(myset.pop())
print(myset.pop())

print(myset)
Output:
0 2 4 6 {8, 10} [Finished in 0.010159231722354889s]

remove() 

The remove() method just like discard() , removes an element from a set if it is a member. However, if the element is not present, remove() will raise a KeyError whereas discard() will not.

Syntax:
myset.remove(x)
x The element to be removed.

The method removes element x from the set , it raises a KeyError exception if the element is not present.

ExampleEdit & Run
myset = {1, 3, 5, 6, 7, 8, 9}

myset.remove(6)
myset.remove(8)
print(myset)

#raises an exception
myset.remove(10)
Output:
{1, 3, 5, 7, 9} KeyError: 10 [Finished in 0.010163878090679646s]

update() 

The update()  method  is used to extend a set  with elements from an iterable object. This means elements from the iterable that  are not already present in the set will be added to the set.

Syntax:
myset.extend(iterable)
iterable An iterable object containing the elements to be added to the set.
ExampleEdit & Run
myset = {1, 3, 5}

#The iterable with the elements
elements = [5, 7, 9, 11]

#add elements to the set
myset.update(elements)
print(myset)
Output:
{1, 3, 5, 7, 9, 11} [Finished in 0.009598023258149624s]

Methods for set operations 

Apart from the usual operations, sets also supports operations that are only unique to them. Such operations includes Union, intersection,  Difference, etc.

In this part we will explore the various methods that are meant for performing set operations.

difference()

The difference() method returns the difference of two or more sets as a new set.  The returned set contains only those items from the first set that are not present in the other sets.

Syntax:
set1.difference(set2 [,set3] [, set4], ...)

The method takes in arbitrary number of sets as arguments and returns a set with the elements of the original set(set1) which are not present in the input sets. Note that if multiple sets are given as inputs,  they will be combined into one set before the operation.

ExampleEdit & Run
set1 = {1, 3, 5, 7, 9, 11}
set2 = {3, 9, 13}
set3 = {7, 9, 11}

diff = set1.difference(set2, set3)
print(diff)
Output:
{1, 5} [Finished in 0.00933760916814208s]

difference_update()

The difference_update() method is similar to the difference() method in that  both returns those elements from one set that are not present in the other(s). However, the  difference_update() method updates the original set by removing elements found in the second set rather than creating a new set.

Syntax:
set1.difference_update(set2 [,set3][, set4]...)

The method removes elements from set1 that are present in the input sets(set2, set3, etc).

ExampleEdit & Run
set1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
set2 = {0, 2, 4}
set3 = {6, 8}

set1.difference_update(set2, set3)
#elements are removed from the original set
print(set1)
Output:
{1, 3, 5, 7, 9} [Finished in 0.009383367840200663s]

intersection()

The intersection() method returns the intersection of two sets as a new set. This is a set that contains all the elements that are common between  two given sets.

Syntax:
set1.intersection(set2 [,set3] [,set4]...)

The method returns elements that are common in both the original set and the input set.

ExampleEdit & Run
set1 = {1, 2, 3, 4, 5}
set2 = {1, 3, 5, 7, 9}

result = set1.intersection(set2)
print(result)
Output:
{1, 3, 5} [Finished in 0.010296056978404522s]

intersection_update()

This method is just like the intersection() method except that it  does not create a new set, instead, it updates the original set.  

ExampleEdit & Run
s1 = {1,2,3,4,5} 
s2 = {3,4,5,6,7} 
s1.intersection_update(s2)

print(s1)
Output:
{3, 4, 5} [Finished in 0.009820783045142889s]

symmetric_difference() 

The symmetric_difference() method returns a new set with elements that are in either of the sets, but not in both.

Syntax:
set1.symmetric_difference(set2) 
ExampleEdit & Run
s1 = {'a', 'b', 'c', 5}
s2 = {3, 5, 7, 'c'}

result = s1.symmetric_difference(s2)
print(result)
Output:
{3, 7, 'a', 'b'} [Finished in 0.009360361378639936s]

symmetric_difference_update()

Just like symmetric_difference() except that it updates the original list rather than creating a new one.

Syntax:
set1.symmetric_difference_update(set2) 
ExampleEdit & Run
s1 = {'a', 'b', 'c', 5}
s2 = {3, 5, 7, 'c'}

s1.symmetric_difference_update(s2)
print(s1)
Output:
{3, 7, 'b', 'a'} [Finished in 0.009624538011848927s]

isdisjoint()

The isdisjoint() method in checks whether two sets have a common item or not. If there are common elements in sets, it returns False otherwise it will return True.

Syntax:
set1.isdisjoint(set2)

It returns True if there is no any common element between set1 and set2, otherwise False

ExampleEdit & Run
s1 = {'a', 'b', 'c'}
s2 = {1, 2, 3, 4}
s3 = {0, 2, 4}

print(s1.isdisjoint(s2))
print(s1.isdisjoint(s3))
print(s2.isdisjoint(s3))
Output:
True True False [Finished in 0.009715872816741467s]

issubset() 

The issubset() method checks if all elements of a set are present in another set (passed as an argument). 

Syntax:
set1.issubset(set2)

It returns True if all elements in set1 are present in set2, otherwise False.

ExampleEdit & Run
evens = {0, 2, 4, 6, 8}
odds = {1, 3, 5, 7, 9}
digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

print(evens.issubset(digits))
print(odds.issubset(digits))
print(evens.issubset(odds))
Output:
True True False [Finished in 0.009279918856918812s]

issuperset()

The issuperset() method checks whether the current set is a superset of the input set. That is if all the elements in the input set are contained within the current set. 

Syntax:
set1.issuperset(set2)

The method returns True if all elements of set2 are in set1, otherwise False.

ExampleEdit & Run
evens = {0, 2, 4, 6, 8}
odds = {1, 3, 5, 7, 9}
digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

print(digits.issuperset(evens))
print(digits.issuperset(odds))
print(evens.issuperset(odds))
Output:
True True False [Finished in 0.00942042376846075s]