SET IN PYTHON

machine learning from scratch

Now, it’s time to look at new data type which is known as set. So, what is Set ? If you remember the mathematical definition of set, simply it is collection of unique elements. Set in Python is the data structure similar to lists or dictionaries that has mathematical definition of set.

Set is the collection of unordered and un indexed object. Also, set is defined by values separated by comma inside braces { }. Sets support a standard collection of operations, including union, intersection, difference, and symmetric .

Unlike tuple and list data type, a set of items is an unordered sequence i.e. The order in which the set objects are listed in a set is not the order in which they are stored because the objects are stored on the basis of their internal hash value.

>>> a = {1,3,4,5}
>>> print(a)
>>> print (type(a))

output:
{1,3,4,5}
<class 'set'>
What are the important properties of a Set ?
  • A collection can be modified even though we can insert a new item and remove an existing item.
  • We can’t save duplicate values into a set.
  • As the order in which items of a set are stored is arbitrary, and there is no definition of using an index location to access an item of a set.

Square bracket is used to define list, small bracket is used define for tuples and lastly we have got one bracket left i.e. curly { } bracket, which is used to define set in python.

>>> s = {35, 60 , 40 ,41, 30 , 60, 35}
>>> print (s)

output :
{30,35,40,41,60}

Surprised with the output ?? Unlike other data types, set in python removes duplicate elements. Lets take another example to be more clear ;

>>> a = {1,1,1,1,1,1,1,1}
>>> print(a)

Expected output : {1,1,1,1,1,1,1,1}
Output obtained : {1}

Since Set is un indexed and unordered collection of objects, indexing is not possible. Lets go though some set operations in python.

Union

The union of two sets A and B is the set of elements which are in A, in B, or in both A and B.

# union
>>> a = {2 ,3,4,5}
>>> b = {5,6,7}
>>> c = a  | b   
>>> print ('union of a and b is :',c)

output: 
union of a and b is : {2, 3, 4, 5, 6, 7}
Intersection

Intersection of two given sets is the largest set which contains all the elements that are common to both the sets. To find the intersection of two given sets A and B is a set which consists of all the elements which are common to both A and B.

# intersection
>>> a = {2 ,3,4,5}
>>> b = {5,6,7}
>>> d = a & b
>>> print ('intersection of a and b is : ',d)

output: 
intersection of a and b is : {5}
Difference

The difference of set B from set A, denoted by A-B, is the set of all the elements of set A that are not in set B. In mathematical term, A-B = { x: x∈A and x∉B} If (A∩B) is the intersection between two sets A and B then, A-B = A – (A∩B)

# difference
>>> a = {2 ,3,4,5}
>>> b = {5,6,7}
>>> e = a - b
>>> print ('difference of a and b is : ',e)

output: 
difference of a and b is : {2, 3, 4}

The symmetric difference, also 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.

# symmetric difference
>>> a = {2 ,3,4,5}
>>> b = {5,6,7}
>>> f = a ^ b 
>>> print ('symmetric difference of a and b is : ',f)

output: 
symmetric difference of a and b is : {2, 3, 4, 6, 7}
MethodsFunctionExample
add ( )adds new items to set name.add(‘sawid’)
update ( ) adds multiple items to setname.update({‘a’,4,5,’b’})
remove ( )removes item from listname.remove(4)
clear( )empties the setname.clear()
pop ( )removes last item from setname.pop()
discard ( )removes items from listname.discard(4)
len ( )returns numbers of items in set len(name)
del ( ) deletes the set completelydel(name)
Example 1:
# add items to list
>>> a = {"diwas", 4,55,66,23," sunita",4,5,7}
>>> a.add("sawid")
>>> print(a)

output :
{66, 4, 5, 7, 'sawid', 'diwas', 55, 23, ' sunita'}
Example 2
 # add multiple items to set 
>>> a = {1,11,111}
>>> a.update({2,3,4})
>>> print (a)

output:
{1, 2, 3, 4, 11, 111}
Example 3
>>> a ={2,3,4}
>>> a.remove(2)
>>> print(a)

output :
{3, 4}

This page is contributed by Diwas & Sunil . If you like AIHUB and would like to contribute, you can also write an article & mail your article to  itsaihub@gmail.com . See your articles appearing on AI HUB platform and help other AI Enthusiast.

About Diwas

🚀 I'm Diwas Pandey, a Computer Engineer with an unyielding passion for Artificial Intelligence, currently pursuing a Master's in Computer Science at Washington State University, USA. As a dedicated blogger at AIHUBPROJECTS.COM, I share insights into the cutting-edge developments in AI, and as a Freelancer, I leverage my technical expertise to craft innovative solutions. Join me in bridging the gap between technology and healthcare as we shape a brighter future together! 🌍🤖🔬

View all posts by Diwas →

Leave a Reply

Your email address will not be published. Required fields are marked *