SET DATA STRUCTURE
▪️ A Python Set is the unordered and unique collection of the data.
▪️ Sets can contain elements of different Data Types Like int, string, Tuple, etc. but cannot contain List, sets, dictionaries.
▪️ Set data structure is used when you want each element to be unique in a large collection of data.
Properties of set datatype:
unordered
immutable
unique
👉 unordered
Set will store the element in an unordered manner i.e. items in the set are not having any defined order such that no direct access to the element is possible. We use an iterator to iterate over all elements of the set.
👉 immutable
Set elements are immutable i.e. can’t be changed but the set is mutable which means we can insert and delete elements from the set.
👉 unique
The frequency of each element in the set is always one. That means no duplicates are allowed to add to a set. Even though you add the duplicates to set it will not be entered into the set created.
INITIALIZATION OF SET:
Set can be initialized in two ways.
Using set function
Using curly braces {}
👉 Using set function:
Initialization: Using the inbuilt set function
Syntax: set name = set (iterable element)
Ex: s = set ()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
INPUT =================================================== #creating a list for iterable items l= [10,20,30,40,50,56,60] #creating a set from list l s=set(l) print(type(s)) print(type(l)) print(s) OUTPUT: ==================================================== <class 'set'> <class 'list'> {40, 10, 50, 20, 56, 60, 30} |
👉 Using curly braces:
Syntax: variable= {element2, ……..element n}
Example:
1 2 3 4 5 6 7 8 9 10 |
INPUT ========================================== #creating a set s={10,20,30,40,50} print(type(s)) print(s) OUTPUT =========================================== <class 'set'> {50, 20, 40, 10, 30} |
While using this type of initialization even if you give the duplicate elements, it will not be taken into the set elements.
Example:
1 2 3 4 5 6 7 8 9 10 |
INPUT ========================================== s={10,20,30,40,50,20} m={12,14,16,18,20,12,18} print("The Elements in set s are:",s) print("The Elements in set s are:",m) OUTPUT ========================================== The Elements in set s are: {50, 20, 40, 10, 30} The Elements in set s are: {16, 18, 20, 12, 14} |
Some important points:
▪️ Observe the elements order in set s and set m in both the input and output as we can clearly see that there is no order i.e unordered one of the properties of set.
▪️ Observe set m we have given the duplicate values but after the printing we get only unique elements in the set i.e unique elements one of the properties of set.
Functions in set
👉 Add: This is used to add a single element to the set.
syntax: setname.add(value)
Example:
1 2 3 4 5 6 7 8 9 10 11 12 |
INPUT ================================================= s={10,20,30,40,50} print("Set before adding the elements:",s) s.add(90) s.add(98) print("Set after adding the elements:",s) OUTPUT ================================================ Set before adding the elements: {50, 20, 40, 10, 30} Set after adding the elements: {50, 98, 20, 90, 40, 10, 30} |
👉 Update: This is used to add multiple elements to a set at a time i.e. iterable elements like list elements to the set .
Syntax: (setname.update(iterable elements) )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
INPUT ================================================= s={10,20,30,40,50} l=[90,80,60,70,50] #updating the set using a iterable data (list) s.update(l) print("Set after updating using the list:",s) #updating the list using range s.update(range(1,10,4)) print("Set after updating using the range:",s) OUTPUT =================================================== Set after updating using the list: {70, 10, 80, 20, 90, 30, 40, 50, 60} Set after updating using the range: {1, 5, 70, 9, 10, 80, 20, 90, 30, 40, 50, 60} |
👉 Pop: used to remove and return a random element from the set
Syntax: (setname.pop( ) )
Example:
1 2 3 4 5 6 7 8 9 10 11 12 |
INPUT =============================================== s={10,2,3,6,8,5,9,18} print(s.pop()) print(s.pop()) print("Set after removing the elements:",s) OUTPUT ================================================ 2 3 Set after removing the elements: {5, 6, 8, 9, 10, 18} |
👉 Remove: This is used to remove a specific element from the set. If the specified element is not present in the set, we get a key error.
Syntax: (setname.remove(value to remove))
Example:
1 2 3 4 5 6 7 8 9 10 |
INPUT ================================================ s={10,2,3,6,8,5,9,18} s.remove(6) s.remove(5) print("Set after removing the elements:",s) OUTPUT ================================================= Set after removing the elements: {2, 3, 8, 9, 10, 18} |
👉 Discard: This is used to remove the specified element from the set. If the specified element is not present, we don’t get any key value error.
Syntax: (setname.discard(value to remove))
Example:
1 2 3 4 5 6 7 8 9 10 11 |
INPUT ================================================================= s={10,2,3,6,8,5,9,18} s.discard(6) #below element that we want to remove is not present in set s s.discard(17) print("Set after removing the elements:",s) OUTPUT ================================================================ Set after removing the elements: {2, 3, 5, 8, 9, 10, 18} |
👉 Clear: This is used to remove all the items from the set
Syntax: setname.clear()
Example:
INPUT
1 2 3 4 5 6 7 8 9 |
INPUT ==================================================== s={10,2,3,6,8,5,9,18} s.clear() print("The items in set after using clear :",s) OUTPUT ==================================================== The items in set after using clear : set() |
👉 Copy: This is used to create a clone object or create a copy of the set
Syntax: variable=setname.copy()
Example:
INPUT
1 2 3 4 5 6 7 8 9 |
INPUT ===================================================== s={10,2,3,6,8,9,5,18} m=s.copy() print("copied set is :",m) OUTPUT ====================================================== copied set is : {2, 3, 5, 6, 8, 9, 10, 18} |
Author : Venkat Vinod Kumar Siram
LinkedIn : https://www.linkedin.com/in/vinodsiram/
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates.
KTExperts is always active on social media platforms.
Facebook : https://www.facebook.com/ktexperts
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform