Nearby lessons

30 of 108

Python - Set Data Type

Introduction

If we want to store a group of values:

  • Without duplicate values
  • Where order is not important

Then we should use the set data type.

Features of Set Data Type

  1. Insertion order is not preserved.
  2. Duplicate values are not allowed.
  3. Heterogeneous values are allowed.
  4. Indexing is not supported.
  5. Mutable.
  6. Growable in nature.

Creating a Set

Set elements are enclosed within curly braces { }.

Example - Creating a Set

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{0, 100, 'durga', 200, 10}

Important Observation

In the above example, the value 10 appears twice.

But the output contains only one 10.

This is because duplicate values are not allowed in a set.

Order is Not Preserved

A set does not maintain the insertion order of elements.

Example - Order is Not Preserved

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{10, 20, 5, 15}

Important Note

The output order may be different every time you run the program.

This is because a set does not preserve insertion order.

Heterogeneous Values Allowed

A set can store different types of values.

Example - Heterogeneous Values

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{True, 10, 10.5, 'durga'}

Indexing is Not Supported

Since a set does not preserve order, indexing is not supported.

Example - Invalid Indexing

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
TypeError: 'set' object does not support indexing

Why Indexing is Not Supported?

Indexing depends on the position of elements.

Since a set does not maintain order, elements do not have fixed positions.

Therefore, indexing cannot be used with sets.

Set is Growable

We can add or remove elements from a set after it is created.

Adding Elements Using add()

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{0, 100, 'durga', 200, 10, 60}

Removing Elements Using remove()

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{0, 'durga', 200, 10}

Iterating Set Elements

We can use a for loop to iterate through all elements of a set.

Example - Iterating a Set

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
20
30

Length of a Set

Use the len() function to find the number of elements in a set.

Example - len() Function

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
3

Membership Operators

We can check whether an element exists in a set using:

  • in
  • not in

Example - Membership Operators

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Creating an Empty Set

Do not use {} to create an empty set.

{} creates an empty dictionary.

Use the set() function instead.

Example - Empty Set

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

Example - Duplicate Removal

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{10, 20, 30}

Real World Usage of Set

Sets are useful when:

  • Duplicate values should be removed.
  • Fast membership testing is required.
  • Unique values need to be stored.

Example - Unique Email Addresses

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{'a@gmail.com', 'b@gmail.com'}

Difference Between List and Set

List Set
Insertion order is preserved. Insertion order is not preserved.
Duplicate values are allowed. Duplicate values are not allowed.
Supports indexing. Does not support indexing.
Uses [ ]. Uses { }.

Example - List

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
[10, 20, 10]

Example - Set

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{10, 20}

Important Notes

  • Set is mutable.
  • Duplicate values are not allowed.
  • Insertion order is not preserved.
  • Indexing is not supported.
  • Heterogeneous values are allowed.
  • Use set() to create an empty set.

Key Points

  • A set stores unique values.
  • Duplicate values are removed automatically.
  • Sets are mutable and growable.
  • Sets support add() and remove().
  • Sets do not support indexing because order is not preserved.
  • Sets are useful for storing unique data and checking membership quickly.

Quick Summary

Feature Description
Data Type set
Mutable Yes
Duplicates Allowed No
Insertion Order Not Preserved
Supports Indexing No
Growable Yes
Symbol { }

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - FrozenSet Data Type