Share via:


▪️ 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
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.


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}

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


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)


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


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


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


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


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


Author    : Venkat Vinod Kumar Siram 

LinkedIn :

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  :

LinkedIn    :

Twitter       :

YouTube   :

Instagram  :

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

Add Comment