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

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.

Intermediate22 min

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

Create unique check no index — set learning path

1. What is a set?

A set automatically removes duplicate values.

List with duplicate 10 becomes unique set
python
s = {10, "RS", 10, 20}

print(s)      # {10, 'RS', 20} — order may vary
print(len(s)) # 3

Note: 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

List ordered tuple immutable set unique

3. Creating sets

Different ways

python
s1 = {10, 20, 30}
s2 = set()
s3 = set([10, 20, 10, 30])
s4 = set("MISSISSIPPI")
Literal empty set() from list and from string

From tuple:

python
print(set((1, 2, 2, 3)))  # {1, 2, 3}

4. The empty set trap

This is another famous Python interview question.

Wrong {} is dict vs correct set() is empty set
python
not_set = {}    # dict!
real_set = set()

print(type(not_set))   # dict
print(type(real_set))  # set

5. Remove duplicates instantly

One of the most practical uses of sets.

python
marks = [85, 90, 85, 78, 90, 92]
unique = set(marks)
print(unique)  # {78, 85, 90, 92} — order may vary
List with duplicate marks becomes unique set

If needed again as a list:

python
back = list(unique)

6. Sets have no index

Sets are unordered, so indexing makes no sense.

Invalid s[0] vs correct 10 in s

Wrong

python
s = {10, 20, 30}
s[0]  # TypeError: 'set' object is not subscriptable

Correct

python
print(10 in s)  # True
print(50 in s)  # False

Use 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.

python
letters = set("MISSISSIPPI")
print(letters)  # {'M', 'I', 'S', 'P'} — order may vary
MISSISSIPPI becomes four unique letter circles

8. 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)
python
s = {10, "RS", (1, 2)}
print(s)  # OK

Invalid:

python
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.

python
visitors = set()

visitors.add("user_42")
visitors.add("user_17")
visitors.add("user_42")  # duplicate ignored

print(len(visitors))  # 2
print(visitors)

Visual dashboard

Unique visitors dashboard — user_42 and user_17 only

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:

python
s = {}       # creates dict — wrong
s = set()    # correct

Mistake 2 — trying to access s[0]:

python
s[0]         # TypeError
10 in s      # correct — membership test

Practice questions

Test yourself before File 09 — Set Methods:

  1. Why does set([1, 2, 2, 3]) contain only three values?
  2. What is the difference between {} and set()?
  3. Why can't you use s[0] on a set?
  4. What is the output of set("BANANA")?
  5. 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…