Lesson 11
set — Unique Unordered Collection
Store only unique values, automatically remove duplicates, and perform fast membership checks using Python's set data type.
Visual reference
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}
s = {10, 20, 30}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
s = {10, 20, 30, 40, 50, 60, 10}
print(s)
print(type(s))Possible output:
{50, 20, 40, 10, 60, 30}
<class 'set'>Notice:
- Duplicate
10disappeared - Order may differ from how you wrote it
- That is normal
Mixed data types
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.
Important: never rely on the printed order of a set.
Chapter 5 — No indexing or slicing
Lists use positions. Sets do not.
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 subscriptableReason: there is no index 0 — only membership in the collection.
Chapter 6 — Adding elements
Use .add(value) to insert one element.
s = {10, 20, 30}
s.add("Python")
print(s)If the value already exists:
s.add(20)
print(s) # unchanged — duplicate ignoredYour 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.
s = {10, 20, 30, 40}
s.remove(30)
print(s){10, 20, 40} # order may varyNote: .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
lst = [10, 20, 30, 10, 20, 40]
unique = set(lst)
print(unique)
print(len(lst), len(unique)){40, 10, 20, 30} # order may vary
6 4From a string
print(set("MISSISSIPPI")){'M', 'I', 'S', 'P'} # order may varyEleven letters collapse to four unique characters.
From a tuple (with duplicates)
print(set((10, 10, 10, 20, 20, 30, 40, 30))){40, 10, 20, 30}Chapter 9 — The empty set trap
Common interview question.
Wrong — empty dict, not a set:
s = {}
print(type(s))<class 'dict'>Correct — empty set:
s = set()
print(type(s))<class 'set'>| Expression | Type |
|---|---|
{10} |
set (one element) |
{} |
dict (empty dictionary) |
set() |
set (empty) |
Single int via constructor (from your notes):
# 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.
print(set([1, 2, 2, 3]))
print(set((10, 20, 30)))
print(set("PYTHON")){1, 2, 3}
{10, 20, 30}
{'P', 'Y', 'T', 'H', 'O', 'N'} # order may varyPractice exercises
1. Remove duplicates from a list
nums = [1, 1, 2, 3, 3]
print(set(nums))2. Add a new element
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
-
Are duplicate values kept in a set?
No — removed automatically. -
Can you access
s[0]?
No — not subscriptable. -
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…