Learning outcomes
By the end of this lecture, you should be able to:- compare list and set behavior
- choose list or set for a given task
- understand duplicate handling and ordering
List vs set at a glance
| Feature | List | Set |
|---|---|---|
| Order | Ordered | Unordered |
| Duplicates | Allowed | Removed automatically |
| Indexing | Supported | Not supported |
| Syntax | [] | {} or set() |
List example
Set example
Use cases
Use list when:- order matters
- duplicates matter
- indexing is needed
- uniqueness matters
- membership checking is primary
- duplicate removal is required
Duplicate removal pattern
Trap: empty set
Exam hints and traps
- set order should not be trusted in answers
- set removes duplicates automatically
{}is empty dictionary, not empty set- list preserves insertion order
Quick practice
- Convert
[1, 1, 2, 3, 3]to unique values. - Which structure is better for roll numbers in attendance check?
- Why can you use
nums[0]with list but not set?
Answer key
set([1, 1, 2, 3, 3])- Set, if only uniqueness and membership matter.
- Set is unordered and has no index positions.
