Introduction To Set Collection Data Type
What is a Set?
A Python Set data type is an unordered Collection of unique elements.
The Collection in a Python Set are Mutable, i.e., elements can be added, or, removed from a Python Set.
Each of the elements in a Python Set must be Immutable, like the Keys of a Python Dictionary.
Why Python Sets are Used?
A common use of Python Set is to efficiently remove the duplicate elements from a series of objects.
How Python Sets are Created?
Python Sets have a literal form that is very similar to the Python Dictionaries.
Python Sets are delimited by curly braces, i.e., {}.
Each of the items within a Set is a single object, rather than a pair joined by a colon.
How to Create a Heterogeneous Set?
Python Sets can be Heterogeneous. That means each of the elements inside a Python Set can be of different data types.
How to Create an Empty Set?
It is not possible to create an empty Set by using an empty curly braces, because, an empty curly braces creates an empty Dictionary.
In order to create an empty Set, the set () constructor is used, where the set () constructor does not take any argument.
set () is the form that Python echoes back as an empty Set.
How to Create a Set from Other Collection Data Types?
Python Sets can be created from other Collection data types, such as — Strings, Lists, using the set () constructor, and, the duplicate elements are discarded.
How to Count the Number of Elements of a Python Set?
It is possible to determine the number of elements in a Set using the built-in Python function, i.e., len ().
How to Iterate Over the Elements of Python Set?
Since Python Sets are Iterables, the for loop can be used to iterate over the elements of Python Sets.
Is It Possible to Have the Same Element Multiple Times in the Declaration of the Set?
It is possible to provide duplicate elements at the time of declaring the Set, but, at the time of displaying the elements of the Set, only the unique elements will be displayed.
How to Add Elements into a Python Set?
A. Add a Single New Element to a Python Set Using add () Method
It is possible to add a single new element to a Python Set by using the add () method.
The add () method takes only one argument, i.e., the element to be added to the Python Set.
Adding an element using the add () method, that already exists in the Python Set has no effect, and, neither does it produces an error.
B. Add Multiple New Elements to a Python Set Using update () Method
It is possible to add multiple new elements to a Python Set using the update () method.
The update () method takes only one argument, i.e., an Iterable series that holds the multiple new elements to be added to the Python Set.
Membership Verification of the Elements in a Set
Although, the order is arbitrary, Membership is a fundamental operation for Sets.
As with other Collection data types, the Membership operation is performed using the IN and NOT IN operators on Sets.
Membership Verification of the Elements in a Set Using IN Operator
To verify that if an element is present in a Set using Membership, the IN operator is used.
If the element is present in the Set, then True is returned.
If the element is not present in the Set, then False is returned.
Membership Verification of the Elements in a Set Using NOT IN Operator
To verify that if an element is not present in a Set using Membership, the NOT IN operator is used.
If the element is not present in the Set, then True is returned.
If the element is present in the Set, then False is returned.
How to Remove the Elements from a Set?
There are two methods available to remove the elements from a Set.
A. Remove the Elements from a Set Using the remove () Method
It is possible to remove the elements from a Set using the remove () method.
The remove () method takes a single parameter, which is the value of the Set element to be deleted, and, removes the element from the Set.
If the value to be deleted, using the remove () method, from a Set, is not present in the Set, the KeyError exception is thrown.
B. Remove the Elements from a Set Using the discard () Method
It is possible to remove the elements from a Set using the discard () method.
The discard () method takes a single parameter, which is the value of the Set element to be deleted, and, removes the element from the Set.
If the value to be deleted, using the discard () method, from a Set, is not present in the Set, the discard () method has no effect, and, neither does it produces an error.
C. Remove the Elements from a Set Using the pop () Method
It is possible to remove any random element from a Set using the pop () method.
The pop () method does not take any parameter, and, returns the deleted element.
D. Remove All the Elements from a Set Using the clear () Method
It is possible to remove all the elements from a Set using the clear () method.