Data Structures
Set
Add, check, or remove an item: O(1) on average. This is instant, no matter how many items you store.
The idea, in plain English
A set is like a guest list at a party door. Each name can appear only once. If you try to add the same name twice, nothing changes. It's already on the list.
How it works
- 1Add items. The set ignores duplicates automatically.
- 2Check if an item exists. You get a fast yes or no answer.
- 3Remove an item, or loop through everything that's left.
When you'd use it
Use a set to remove duplicates from a list, quickly check 'have I seen this before?', or track unique visitors to a page.
Common beginner mistakes
- Don't expect a set to remember the order you added items. If order matters, sort the items before printing.
- Don't assume printing a set always shows the same order. Python sets don't guarantee any order.
Try it — edit and run
Click the code to edit · press ⌘/Ctrl+↵ to run
Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.
Expected output — hit Run to try it
Unique: amy bo cy
Count: 3
Has bo: yes
Has bo: noSee it in motion
Watch it dedupSetAdd, check, or remove an item: O(1) on average. This is instant, no matter how many items you store.
Incoming names
The set (unique only)
empty
Ready. Press play to build the set.
0/6
Processed
0
Unique
0
Duplicates
—
Test
Added / hitConsidering / matchRejected / missSettled
Not sure this is the right topic? See the learning paths → or where this leads →