Sets: Beginner
No positions, so no indexing
A list remembers where each item sits, so scanned_list[0] is a meaningful request: the first thing scanned. A set remembers only which values are present. There's no first element, so asking for [0] on a set raises an error. The order Python prints a set in comes from its internal storage layout, and your code must never lean on it.
Subscriptable means 'accepts square brackets after it'. A list is subscriptable, as are strings and dictionaries. A set is not.
Membership in 1 step
The in operator works on both, but the work behind it differs. A list compares your value against item after item from the front until one matches or the list ends. A set computes a slot from the value itself and looks in that 1 slot. At 10 items you won't notice a difference. At 10 million, the list might make 10 million comparisons before saying False, while the set still takes a handful of steps.
Side by side
- Keeps items in the order you added them
- Stores the same value as many times as you add it
- Reaches an item by position: items[0]
- in compares against items one by one
- Has no order you can rely on
- Keeps 1 copy of each value, always
- No positions, so square brackets raise an error
- in jumps straight to the value's slot
| Task | List | Set |
|---|---|---|
| Hold 7 three times | `[7, 7, 7]` holds 3 items | `{7, 7, 7}` holds 1 item |
| Get the first item | `items[0]` | No such thing |
| Count what is stored | `len` counts every copy | `len` counts distinct values |
| Print it | Square brackets, insertion order | Braces, storage order |
When to pick a set over a list
What does this program print?
That question leaned on 2 facts: repeats collapse when the set is built, and len counts what survived. Add in and you have the pattern behind most everyday set work: write the raw values as a set literal, let the repeats fall away, then ask len for the distinct count and in about any single value. Here a door scanner logged 6 badge swipes from 4 different people.
This prints 4, then True, then False. Ids 301 and 302 each arrived twice, so the 6 ids in the literal shrink to 4 members. A list of the same ids would report 6 from len, which counts swipes rather than people, and that gap is the reason to choose the set here.
Write braces around values when you know the values while writing the code. Call the set() function on an iterable when the values already live in some other collection and you want its distinct members.
Braces around known values
Each value appears once in the result whether you typed it once or twice. Writing {2, 3, 5, 7} and {7, 5, 3, 2, 2} produces equal sets, because a set is defined by its members alone, never by how many times or in what order they were listed.
The empty set is set(), never {}
Braces reached Python as dictionary syntax first, so empty braces produce an empty dictionary. There's no brace spelling for a set with nothing in it. Call set() with no argument, and read the type back whenever you doubt which one you are holding.
Printing an empty set shows set() rather than braces, for the same reason: braces with nothing between them would read as a dictionary. You'll see this spelling again whenever a set runs out of members.
set() walks any iterable
An iterable is anything a for loop can step through: a list, a string, a range, a dictionary. set() steps through it the same way and adds each item it meets. Repeats collapse as they arrive. A dictionary contributes its keys, because keys are what a for loop over a dictionary yields.
The list [12, 7, 12, 3, 7, 7] became {3, 12, 7}: 3 distinct values survived, and the printed order matches neither the list nor numeric order. That order is the storage layout, nothing more. The string "mississippi" has 11 characters but only 4 distinct letters, m, i, s, p. The dictionary gave up its 3 keys and none of its values.
| Source | What set() steps through | Result |
|---|---|---|
| `[1, 1, 2]` | Each list item | `{1, 2}` |
| `"aab"` | Each character | `{"a", "b"}` |
| `range(3)` | 0, 1, 2 | `{0, 1, 2}` |
| `{"x": 1, "y": 2}` | The keys only | `{"x", "y"}` |
| `42` | Nothing: an integer has no items | `TypeError` |
An integer is a single value with nothing to step through, so set(42) has no items to collect and Python says so. Either {42} or set([42]) makes a set holding 42, because each gives set() something it can walk.
One string or its characters?
{"hello"} has 1 member, the whole word. set("hello") has 4 members, one per distinct letter: h, e, l, o. Both are valid, and they answer different questions, so pick the spelling that matches the question you are asking.
- ▸
{"abc"}: 1 member, the string itself - ▸
set("abc"): 3 members, one per character - ▸
set(["abc"]): 1 member, because the list being walked holds 1 item
set(variable). When you're typing the values by hand, write braces. The 1 exception is zero values, which needs set().What does this line print?
The 2 sources you will turn into sets most often are a list of raw records and a dictionary used as a lookup. Both go through the same set() call, and the results compare directly, because a set built from a dictionary holds plain keys.
This prints 3 and then False. The basket names 4 codes with "B2" twice, so 3 distinct codes remain. The prices never made it into price_codes; it holds only the keys "A1", "B2" and "C3". So "D4" fails the test, and the program has found an item it can't charge for.
A set does deduplication for free, but the data has to get into the set first. add puts in 1 value, and update puts in every value in an iterable. Neither raises an error when a value is already present. The set stays as it was, and the program moves on.
add: one value, no complaints
That was 4 add calls and 2 distinct visitors. The third and fourth calls found "u17" present and did nothing. Compare a list: 4 append calls would store 4 items, and counting distinct visitors would need a loop that checks each new id against everything stored before it.
Inside a loop, add becomes a filter that remembers. The program below walks a list of numbers, offers each even one to a set, and counts what survived.
numbers = [4, 7, 2, 9, 4, 6, 2, 8, 6]unique_evens = set()for num in numbers:if num % 2 == 0:unique_evens.add(num)print("Unique even count:", len(unique_evens))print("Unique evens:", unique_evens)
Each even number is offered to the set; repeats leave it unchanged.
The 9 numbers include 6 even ones, only 4 of them distinct. The set never needed an if num not in unique_evens check before adding, because add carries that check inside it. The pattern 'add everything, then len' answers 'how many distinct?' with no counters and no nested loop.
The loop and set(log) arrive at the same 3 actions. The loop version is worth its extra lines when you need to do something else per item, such as skip some of them; otherwise set(log) says it shorter.
update: many values at once
update takes an iterable and adds each of its items, one by one, the same way set() does while building. Writing seen.update(items) is the same as a for loop calling seen.add(item) on each item, in 1 line.
The first update offered [2, 3, 4]. 2 was already present, so only 3 and 4 were added. The second offered 3 through 6, and only 5 and 6 changed anything. Updating with an empty list touches nothing. The count ends at 6.
A string is 1 value to add and 4 to update
update walks its argument. A string walks as characters, so tags.update("sale") adds 4 separate members: "s", "a", "l", "e". To add the word as 1 tag, use tags.add("sale") instead; wrapping it in a list, tags.update(["sale"]), also works.
- Takes exactly 1 value
- Puts that value in as 1 member
- add("sale") adds the word
- add([1, 2]) raises an error: a list can't be a member
- Takes something a loop can walk
- Puts each item in as its own member
- update("sale") adds 4 letters
- update([1, 2]) adds 1 and 2
add when your hand holds 1 value and update when it holds a collection. Passing a collection to add is the mistake the last section of this lesson explains in full.Dedupe and keep arrival order
set(items) removes repeats and forgets which came first. When first appearance matters, a list recording the order and a set answering 'seen before?' work as a pair. The set does the fast check; the list grows only when the check says new.
items = ["apple", "kiwi", "apple", "fig", "kiwi", "plum", "apple"]seen = set()unique_ordered = []for item in items:if item not in seen:seen.add(item)unique_ordered.append(item)print("Original:", items)print("Unique, in order:", unique_ordered)
The set answers the membership test; the list keeps arrival order.
The loop receives 7 items, 4 of them distinct. Each one is tested against seen in 1 step. The list unique_ordered ends as ["apple", "kiwi", "fig", "plum"], first appearances in the order they came, which set(items) alone couldn't promise.
How many members does s hold at the end?
Lists and sets grow through different method names, and mixing them up is one of the first errors people meet with sets. A list has append because new items go on its end. A set has no end, so it has no append, and calling it stops the program with AttributeError: 'set' object has no attribute 'append'. The reverse holds too: a list has no add.
| Collection | Method that adds 1 value | What a repeat does |
|---|---|---|
| List | `items.append(x)` | Stored again as a new item |
| Set | `items.add(x)` | Changes nothing |
When an error names append on a set, the fix is the method name alone. Everything else, including the final len, stays as written.
A set has 4 methods that take values out. remove and discard each take out 1 named value and differ only in what happens when it is absent. pop takes out some member and hands it back. clear empties the set. To check what's there before or after, use in and not in.
remove raises, discard forgives
remove(5) found 5 and took it out. discard(5) then looked for 5 and returned without complaint when it wasn't there. discard(99) did the same. Swap that last discard for remove and the program stops.
A KeyError names the value that was missing, the same error a dictionary raises for a missing key, because a set is stored much like a dictionary that has keys and no values. Use remove when absence means something went wrong upstream and you want the program to stop and say so. Use discard when absence is an ordinary state, such as cancelling a reservation that was already cancelled.
s.remove(x) in a colleague's code tells you the author believes x must be there. Reading s.discard(x) tells you they expect it might not be. Choose the one that documents your own belief.Testing before and after
A removal shows up in 2 places: the removed value now fails the in test, and len drops by 1. Nothing else moves, because nothing in a set has a position to shift.
Guarding with in
The if word in blocked guard makes remove safe, which is what discard does on its own. Write the guard when the other branch has work of its own, such as printing a message; otherwise discard says the same thing in 1 line.
A loop of discard calls handles a batch of removals where some may not apply. Here 3 guests cancel, and 1 of them was never on the list.
guests = {11, 12, 13, 14}cancelled = [12, 15, 13]for guest in cancelled:guests.discard(guest)print("Still coming:", guests)print("Seats needed:", len(guests))
Each cancellation is offered to discard; a guest never on the list causes no error.
Guest 15 was not in the set, and discard passed over it without raising anything. The set keeps 2 guests, and the seat count follows from len.
pop takes something, clear takes everything
pop removes 1 member and returns it, but you don't choose which. The set picks from its storage layout, so on a set with several members, treat the result as 'some member', never 'the first one' or 'the smallest'. Calling pop on an empty set raises KeyError. clear removes every member and leaves set().
With 1 member there is nothing to guess: pop returns 4. After clear, printing shows set(), the spelling for an empty set, since {} would read as an empty dictionary.
| Call | Effect | If the value is absent |
|---|---|---|
| `s.remove(x)` | Takes x out | `KeyError` |
| `s.discard(x)` | Takes x out | Nothing happens |
| `s.pop()` | Takes out some member and returns it | `KeyError` only when the set is empty |
| `s.clear()` | Takes out every member | Nothing to take, no error |
| `x in s` | Reports presence | Returns `False` |
- Use discard for batches where some values may already be gone
- Use remove when a missing value signals a bug worth stopping for
- Test with in when the other branch has its own work to do
- Treat pop as returning the first or smallest member
- Call remove in a loop over values you haven't confirmed are present
- Write {} hoping for an empty set
What does this program print?
Seat releases arrive the way the cancellations did: a list of values, some of which were never taken. Choosing the method is choosing what an absent value means. If a release for a seat nobody held is ordinary, discard lets the loop finish. If it means 2 systems disagree about the seating chart, remove stops at the first bad value and names it in the KeyError. After the loop, not in confirms each release took effect.
This prints 2 True. Seat 30 was never held and the loop passed over it; seat 9 left, so 9 not in held is True.
The error says unhashable. Hashing is the step that turns a value into the slot number the set stores it under, the same step that lets in finish in 1 look. The message means Python declined to compute that number for a list.
Why a changing value cannot be a member
Suppose a set did accept [1, 2]. It would compute a slot from the contents 1 and 2 and file the list there. Then you run my_list.append(3). The list in the set now reads [1, 2, 3], but it still sits in the slot computed for [1, 2]. Ask [1, 2, 3] in my_set and the set looks in the slot for [1, 2, 3]. That slot is empty, so it says False about a value it holds. Rather than allow that, Python refuses to hash its built-in types that can change after creation, which includes lists and dictionaries as well as sets themselves. Integers and floats cannot be altered in place, and neither can strings or booleans, so their slot never goes stale.
| Value | Can it change after creation? | Set accepts it? |
|---|---|---|
| `42` | No | Yes |
| `2.5` | No | Yes |
| `"abc"` | No | Yes |
| `True` | No | Yes |
| `[1, 2]` | Yes, `append` and more | No |
| `{"a": 1}` | Yes, keys come and go | No |
| `{1, 2}` | Yes, `add` and `discard` | No |
Mixed types in 1 set are fine: 3 members of 3 types. The failure comes at the add([8]) line, after the 2 prints have already run. Read the output in order: 2 printed lines, then the traceback. An error stops the program at the line that raised it, and nothing earlier is undone.
Records: store the id, keep the dict elsewhere
User 3 sent 2 events, and the set holds 3 once. The dictionaries stay in events, available for anything else the program needs; the set holds only what identifies them, which is all a distinct count requires.
Sets inside sets
A set can't hold a set for the same reason it can't hold a list: add and discard would change its contents after filing. Python does offer a frozen kind of set that accepts no changes and so can be a member, and the advanced sets lesson covers it. For now the rule to carry is 1 sentence: if you could change it in place, it cannot be a set member.
- ▸Can you change it in place by calling
appendorupdateon it? Then no. - ▸Is it a number or a string? Is it
TrueorFalse? Then yes. - ▸Is it a record with several fields? Add the 1 field that identifies it.
Which line raises an error, and why?
The error in that question has a common cousin. You hold a set and a list of new values for it, and you pass the whole list to add. Python reads that as a request to store the list itself as 1 member, and a list can change, so the call raises TypeError: unhashable type: 'list'. What you meant was to store each item of the list, which is the job update does.
This prints 3: "LAX" joins and "SFO" was already there. Written as codes.add(new_codes), the same program stops at line 3 and never reaches the print.
> At DoorDash, an on-call engineer wants to know how many distinct restaurants received at least 1 order during a Friday dinner rush. The order events arrive as a list of dictionaries, each carrying a restaurant id, and a busy restaurant appears hundreds of times. A set of ids, filled by one loop, answers the question with a single len call.
event["restaurant_id"] rather than the event itself.add inside the loop collapses the hundreds of repeat orders from one restaurant into 1 member, with no check written by hand.discard in a loop, since some of them may have had no orders at all.in confirms whether a specific restaurant took part, in 1 step regardless of how many events arrived.s[0] raises an error and printed order means nothing.set(iterable) for data in a variable; an empty set is set(), because {} is a dictionary.add puts in 1 value and update puts in each item of an iterable; a value already present changes nothing.remove raises KeyError on a missing value, discard stays quiet, pop returns some member, clear empties the set.A collection that keeps 1 copy of everything and answers 'have I seen this?' in a single step.
- Category
- Python
- Difficulty
- beginner
- Duration
- 55 minutes
- Challenges
- 19 hands-on challenges
Topics covered: What is a Set?, Creating Sets, Automatic Duplicate Removal, Removing Elements from Sets, What Can Be in a Set?
Lesson Sections
- What is a Set? (concepts: Sets)
A set holds each value at most once and gives those values no positions. You put values in, you ask whether a value is present, and you count how many distinct values you hold. A list can store the same ticket number 5 times; a set given the same 5 numbers keeps 1 copy of each and drops the rest on its own, with no code from you to check for repeats. Both collections received the same 5 numbers. The list reports 5 scans because it recorded every arrival. The set reports 3 distinct tickets becaus
- Creating Sets (concepts: Sets)
Braces around known values The empty set is set(), never {} set() walks any iterable One string or its characters?
- Automatic Duplicate Removal (concepts: Sets)
add: one value, no complaints update: many values at once A string is 1 value to add and 4 to update Dedupe and keep arrival order
- Removing Elements from Sets (concepts: Sets)
remove raises, discard forgives Testing before and after Guarding with in pop takes something, clear takes everything
- What Can Be in a Set? (concepts: Sets)
Numbers and strings can be set members, and so can booleans. Lists and dictionaries cannot, and neither can another set. The rule behind the 2 groups is about change: a value that can be altered after it is created can't be a set member. Why a changing value cannot be a member Records: store the id, keep the dict elsewhere You'll often have a list with 1 dictionary per event and need the distinct users behind them. The dictionaries can't go in a set, and they don't need to. Pull out the field th