Lesson 08
Set Basics — Uniqueness, Creation, and Limitations
Create Python sets from lists and strings, remove duplicates, use membership checks, and learn why sets have no indexing.
What you will learn
Unique Values · Hashable · Membership · Deduplication
A set is an unordered collection that stores only unique values. The biggest use cases are removing duplicates and performing fast membership checks.
| Property | Set behavior |
|---|---|
| Duplicates | Removed automatically |
| Order | Not guaranteed |
| Index / slice | Not supported |
| Membership | Fast with in |
| Elements | Must be hashable |
Memory formula: Add → Unique → Check → No Index
After File 07 — Tuple Methods, you move from ordered sequences to unordered unique collections. Intro: set — Unique Unordered Collection.
Learning roadmap
1. What is a set?
A set automatically removes duplicate values.
s = {10, "RS", 10, 20}
print(s) # {10, 'RS', 20} — order may vary
print(len(s)) # 3Note: Order is not guaranteed, so your output may appear differently.
2. Set vs list vs tuple
| Feature | List | Tuple | Set |
|---|---|---|---|
| Ordered | Yes | Yes | No |
| Duplicates | Yes | Yes | No |
| Mutable | Yes | No | Yes |
| Indexing | Yes | Yes | No |
| Best for | Dynamic data | Fixed data | Unique values |
Visual memory card
3. Creating sets
Different ways
s1 = {10, 20, 30}
s2 = set()
s3 = set([10, 20, 10, 30])
s4 = set("MISSISSIPPI")From tuple:
print(set((1, 2, 2, 3))) # {1, 2, 3}4. The empty set trap
This is another famous Python interview question.
not_set = {} # dict!
real_set = set()
print(type(not_set)) # dict
print(type(real_set)) # set5. Remove duplicates instantly
One of the most practical uses of sets.
marks = [85, 90, 85, 78, 90, 92]
unique = set(marks)
print(unique) # {78, 85, 90, 92} — order may varyIf needed again as a list:
back = list(unique)6. Sets have no index
Sets are unordered, so indexing makes no sense.
Wrong
s = {10, 20, 30}
s[0] # TypeError: 'set' object is not subscriptableCorrect
print(10 in s) # True
print(50 in s) # FalseUse membership (
in) instead of indexing.
Item assignment and del s[i] also fail — only del s removes the whole variable.
7. Create sets from strings
Every character becomes one unique element.
letters = set("MISSISSIPPI")
print(letters) # {'M', 'I', 'S', 'P'} — order may vary8. Hashable vs unhashable
Only hashable (generally immutable) objects can be stored inside a set.
| Allowed | Not allowed |
|---|---|
int, float, str |
list |
tuple (if items hashable) |
dict |
frozenset |
set (mutable) |
s = {10, "RS", (1, 2)}
print(s) # OKInvalid:
s = {[1, 2]} # TypeError: unhashable type: 'list'See Hashability and Mixed Collections for the full rules.
Mini project — website unique visitors
Imagine you are tracking users visiting a webpage.
visitors = set()
visitors.add("user_42")
visitors.add("user_17")
visitors.add("user_42") # duplicate ignored
print(len(visitors)) # 2
print(visitors)Visual dashboard
Notice how adding "user_42" twice does not increase the count. Full mutating methods come in File 09 — Set Methods.
Set category
| Mutable | Immutable |
|---|---|
set |
frozenset |
One-page revision sheet
| Task | Syntax |
|---|---|
| Create set | {1, 2, 3} |
| Empty set | set() |
| From list | set(lst) |
| From string | set("HELLO") |
| Remove duplicates | set(data) |
| Membership | x in s |
| Size | len(s) |
Common interview mistakes
Mistake 1 — using {} for an empty set:
s = {} # creates dict — wrong
s = set() # correctMistake 2 — trying to access s[0]:
s[0] # TypeError
10 in s # correct — membership testPractice questions
Test yourself before File 09 — Set Methods:
- Why does
set([1, 2, 2, 3])contain only three values? - What is the difference between
{}andset()? - Why can't you use
s[0]on a set? - What is the output of
set("BANANA")? - Why is a list not allowed inside a set?
Knowledge check
Q1: Why only three values in set([1, 2, 2, 3])?
Answer: Sets store unique values — duplicate 2 is removed.
Q2: {} vs set()?
Answer: {} is an empty dict; set() is an empty set.
Q3: Why no s[0]?
Answer: Sets are unordered — there is no index position.
Q4: set("BANANA")?
Answer: {'B', 'A', 'N'} — three unique letters (order may vary).
Q5: Why no list inside a set?
Answer: Lists are mutable/unhashable — set elements must be hashable.
Loading playground…