Newby Coder header banner

Python Sets

Sets In Python

Creating set using set() function

A set can be created by passing a list of values to the built-in set() function or by declaring a list of values inside curly braces {}

>>> set_from_list = set([4, 9, 5, 8])
>>> print(" Set : ",set_from_list)
 Set :  {8, 9, 6, 0}

The order of elements have been changed in the result because set is an unordered collection of data unlike list and tuple

Creating set using curly braces {}

>>> curly_braces_set = {3, 4.5}
>>> print(" Set : ", curly_braces_set)
 Set :  {3, 4.5}

Set With Mixed Data Types

A set can have any number of elements but the datatype for the elements should be hashable like integer, float, string etc

A list can have a set as an element but not vice versa

>>> mixed_set = {'string in a set', 50.05, 1000}
Set :  {1000, 50.05, 'string in a set'}

Creating Empty Set

For creating empty set, set( ) method has to be used without any argument

>>> empty_set = set()
>>> print("Set : ", empty_set)
Set :  set() 

Empty set can be created using curly braces { } because empty curly braces make empty dictionary in python

Set Removes Duplicate Values

Set also remove duplicate values

>>> set1 = set([1, 1, 3, 11, 6, 11, 1, 2])
>>> print("Set :", set1)
Set : {1, 2, 3, 6, 11}

Above example shows a set set1 is declared by passing a list which contains duplicate values

The resultant set doesn't contain any duplicate element

Manipulating Sets In Python

Adding values to a Set

To add more values to a set, add( ) function is used

>>> print("Initial set :", set1)
Initial set : {1, 3, 5}
>>> set1.add(7)
>>> print("Updated set :",set1)
Updated set : {1, 3, 5, 7} 

Removing values from a Set

Methods remove() or discard() can be used to remove elements from a set

remove( ) method

>>> set1 = set([1,3,5])
>>> print("Initial set :", set1)
Initial set : {1, 3, 5}
>>> set1.remove(3)
>>> print("Updated set :",set1)
Updated set : {1, 5} 

discard() method

discard() method is also used to remove value from a set

The difference between discard() and remove() method is that –

>>> print("Initial set :", set1)
Initial set : {1, 3, 5}
>>>
>>> set1.discard(7)
>>> set1.discard(3)
>>> print("Updated set", set1)
Updated set {1, 5} 

Updating a Set

A set can be updated by using update( ) method which allows add multiple values to a set at one time

>>> set1 = set([1, 3, 5])
>>> print("Initial set :", set1)
Initial set : {1, 3, 5}
>>>
>>> set1.update(["One", "Three", "Five"])
>>> print("Updated set :", set1)
Updated set : {1, 3, 5, 'Three', 'One', 'Five'} 

A set can also be updated with another set

>>> set1 = set([1, 3, 5.2])
>>> print("Initial set :",set1)
Initial set : {1, 3, 5.2}
>>> set2 = set([1, "Three", "Five"])
>>>
>>> set1.update(["Seven", "9", 3], set2)
>>> print("Updated set :", set1)
Updated set : {1, 3, 5.2, '9', 'Three', 'Seven', 'Five'} 

Python Set Operations

Following operations can be performed on sets

Set Union Operation

Union of two sets is a resultant set which contains all elements of both sets

cl-python-sets-union

Above figure shows union of two sets X and Y which is the set of all elements from both sets

Python set provides the method union() to perform union operation

>>> set1 = set([1, 3, 5, 7])
>>> set2 = set([2, 4, 5, 6, 7])
>>> print("set1:", set1)
set1: {1, 3, 5, 7}
>>> print("set2:", set2)
set2: {2, 4, 5, 6, 7}
>>>
>>> set3 = set1.union(set2)
>>> print("Union:", set3)
Union: {1, 2, 3, 4, 5, 6, 7} 

Set Intersection Operation

Intersection of two sets results in the set of common elements among the sets

cl-python-sets-intersection

In above figure, the intersection of sets X and Y is highlighted

>>> set1 = set([1, 3, 5, 7])
>>> set2 = set([2, 4, 5, 6, 7])
>>> print("set1:", set1)
set1: {1, 3, 5, 7}
>>> print("set2:", set2)
set2: {2, 4, 5, 6, 7}
>>>
>>> set3 = set1.intersection(set2)
>>> set3
{5, 7}

Set Difference Operation

Set difference is the set of elements which are present in one set but not in the other

cl-python-sets-difference

So difference of set X from Y (X-Y) is the set of elements in set X which are not in set Y

Similarly (Y-X) is the set of elements that are in set Y but not in set X

In python, set difference operation can be performed using difference() method

>>> set1 = set([1, 3, 5, 7])
>>> set2 = set([2, 4, 5, 6, 7])
>>> print("set1:", set1)
set1: {1, 3, 5, 7}
>>> print("set2:", set2)
set2: {2, 4, 5, 6, 7}
>>>
>>> set1.difference(set2)
{1, 3}
>>> set2.difference(set1)
{2, 4, 6}