Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 11

set — Unique Unordered Collection

Store only unique values, automatically remove duplicates, and perform fast membership checks using Python's set data type.

Beginner15 min

Visual reference

Unique marbles — no duplicates in the bagUnique marbles — no duplicates in the bag
Python sets overviewPython sets overview
Set operations — union, intersection, and moreSet operations — union, intersection, and more

What you will learn

  • Create empty and non-empty sets
  • Understand automatic duplicate removal
  • Why sets have no indexing or slicing
  • Add and remove with .add() and .remove()
  • Use sets to deduplicate lists and strings

Chapter 1 — What is a set?

A set is a collection that stores only unique values. Insert the same value twice — Python keeps one copy.

Purpose (your notes): store multiple values (same or mixed types) with unique values only — duplicates are not allowed.

Definition: an unordered, mutable collection of unique elements.

Syntax: varname = {Val1, Val2, ..., Val-n}

python
s = {10, 20, 30}
Bag of unique values — no duplicate marbles

Think of a set as a bag of unique marbles — another red marble does not create a second red in the set.

Membership: value in s is a common, fast check (try 20 in s in the playground).

Chapter 2 — Properties of a set

Property Behavior
Order Not guaranteed (no insertion order)
Duplicates Removed automatically
Indexing Not supported
Slicing Not supported
Item assignment s[i] = x Not supported
Add / remove Yes — .add(), .remove(), etc.

Your notes: a set is mutable for add/delete at the same address (id often unchanged), but not mutable via item assignment like a list.

Chapter 3 — Creating sets

Non-empty set

python
s = {10, 20, 30, 40, 50, 60, 10}

print(s)
print(type(s))

Possible output:

text
{50, 20, 40, 10, 60, 30}
<class 'set'>

Notice:

  • Duplicate 10 disappeared
  • Order may differ from how you wrote it
  • That is normal

Mixed data types

python
mixed = {
    10,
    "Python",
    34.56,
    True,
    2 + 3j,
}

print(mixed)

Sets can hold int, str, float, bool, complex, and more in one object.

Chapter 4 — Why order changes

A set is unordered — elements are not stored by position like a list.

Same set — different display order between runs

Important: never rely on the printed order of a set.

Chapter 5 — No indexing or slicing

Lists use positions. Sets do not.

python
s = {10, 20, 30}

# print(s[0])
# TypeError: 'set' object is not subscriptable

# s[0] = 100
# TypeError: 'set' object does not support item assignment

# s[0:3]
# TypeError: 'set' object is not subscriptable

Reason: there is no index 0 — only membership in the collection.

Chapter 6 — Adding elements

Use .add(value) to insert one element.

python
s = {10, 20, 30}

s.add("Python")

print(s)

If the value already exists:

python
s.add(20)
print(s)  # unchanged — duplicate ignored

Your notes: id(s) often stays the same after .add() — you are updating the same set object.

Chapter 7 — Removing elements

Use .remove(value) to delete a value that must exist.

python
s = {10, 20, 30, 40}

s.remove(30)

print(s)
text
{10, 20, 40}   # order may vary

Note: .remove() raises KeyError if the value is missing (later you may learn .discard() for silent removal).

Chapter 8 — Removing duplicates

A top use case for sets is deduplication.

From a list

python
lst = [10, 20, 30, 10, 20, 40]

unique = set(lst)

print(unique)
print(len(lst), len(unique))
text
{40, 10, 20, 30}   # order may vary
6 4

From a string

python
print(set("MISSISSIPPI"))
text
{'M', 'I', 'S', 'P'}   # order may vary

Eleven letters collapse to four unique characters.

MISSISSIPPI → unique letters

From a tuple (with duplicates)

python
print(set((10, 10, 10, 20, 20, 30, 40, 30)))
text
{40, 10, 20, 30}

Chapter 9 — The empty set trap

Common interview question.

Wrong — empty dict, not a set:

python
s = {}

print(type(s))
text
<class 'dict'>

Correct — empty set:

python
s = set()

print(type(s))
text
<class 'set'>
Expression Type
{10} set (one element)
{} dict (empty dictionary)
set() set (empty)

Single int via constructor (from your notes):

python
# set(10)  # TypeError: 'int' object is not iterable
print(set([10]))
print(set((10,)))
print({10})

Chapter 10 — Creating sets from iterables

Syntax: set(object) when object is iterable.

python
print(set([1, 2, 2, 3]))
print(set((10, 20, 30)))
print(set("PYTHON"))
text
{1, 2, 3}
{10, 20, 30}
{'P', 'Y', 'T', 'H', 'O', 'N'}   # order may vary

Practice exercises

1. Remove duplicates from a list

python
nums = [1, 1, 2, 3, 3]
print(set(nums))

2. Add a new element

python
s = {10, 20}
s.add(99)
print(s)

3. Why is {10} a set but {} is not?

{} is reserved for an empty dictionary; set() creates an empty set. {10} is a non-empty set literal with one item.

Quick knowledge check

  1. Are duplicate values kept in a set?
    No — removed automatically.

  2. Can you access s[0]?
    No — not subscriptable.

  3. How do you create an empty set?
    set().

Summary

  • A set is an unordered, mutable collection of unique values.
  • Duplicates are dropped; order is not reliable.
  • No indexing or slicing — use membership (in) and methods like .add() / .remove().
  • set() = empty set; {} = empty dict.

Next lesson: frozenset — immutable unique collection.

Loading playground…