SET DATA STRUCTURE

Share via:

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 ()

👉 Using curly braces:
Syntax: variable= {element2, ……..element n}
Example:

While using this type of initialization even if you give the duplicate elements, it will not be taken into the set elements.
Example:

 

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:

 

👉 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) )

 

👉 Pop: used to remove and return a random element from the set
Syntax: (setname.pop( ) )
Example:

 

👉 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:

 

👉 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:

 

👉 Clear: This is used to remove all the items from the set
Syntax: setname.clear()
Example:
INPUT

 

👉 Copy: This is used to create a clone object or create a copy of the set
Syntax: variable=setname.copy()
Example:
INPUT

 

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

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Add Comment