Introduction To Set Collection Data Type

Oindrila Chakraborty
6 min readDec 18, 2022

--

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.

Create a Python Set of Integers
Create a Python Set of Strings

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.

Create a Heterogeneous Python Set

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.

Create an Empty Python 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.

Create a Python Set From a String
Create a Python Set From a List

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

Count the Number of Elements of a Python Set

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.

Iterate Over the Elements of a Python Set Using the for Loop

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.

Add One New Element to a 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.

Add One New Element to a Python Set, that Already Exists
Not Possible to Add More Than One New Elements to a Python Set

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.

Add More Than One New Elements as a List to a Python Set
Add More Than One New Elements as a Tuple to a Python Set
Add More Than One New Elements as a Set to a 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.

Verify If an Element is Present in a Set Using the IN Operator

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.

Verify If an Element is Not Present in a Set Using the NOT IN Operator

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.

“Remove the Element, i.e., “Rama”, from the Set Using the remove () Method

If the value to be deleted, using the remove () method, from a Set, is not present in the Set, the KeyError exception is thrown.

Remove the Element, i.e., “Swaralipi”, Which is Not Present, in the Set Using the remove () Method

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.

Remove the Element, i.e., “Rama”, from the Set Using the discard () Method

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.

Remove the Element, i.e., “Swaralipi”, Which is Not Present, in the Set Using the discard () Method

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.

Remove a Random Element from the Set Using the pop () Method

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.

Remove All the Elements from the Set Using the clear () Method

--

--

Oindrila Chakraborty
Oindrila Chakraborty

Written by Oindrila Chakraborty

I have 12+ experience in IT industry. I love to learn about the data and work with data. I am happy to share my knowledge with all. Hope this will be of help.

No responses yet