Imagine you collect the email addresses of everyone who signs up for an event. Some people sign up twice, and some use the same address at different times. You want each address exactly once. Or imagine two lists of students, one for the maths club and one for the science club, and you want to know who belongs to both. A Python set is made for exactly these jobs.
A set is a collection of unique items. It throws away duplicates for you, it can tell you in an instant whether something is inside it, and it supports the operations you remember from school maths: union, intersection and difference. Sets are small, fast and very handy for cleaning data and comparing groups.
In this tutorial, we explain what a set is and how to create one, including the empty set trap that catches almost every beginner. We cover how to add and remove items, with the difference between remove and discard. Then we look at the set operations: union, intersection, difference, and the subset and superset checks, with all the directions shown. We end with removing duplicates, frozen sets, common mistakes and the full program. Every output comes from a real run on Python 3.13.
A set is an unordered collection of unique items written in curly brackets.
set() turns another collection, such as a list, into a set.set splits into single characters, and the repeated l in hello appears once. We used sorted here because the order of text items in a set can change from run to run.Empty curly brackets make a dictionary, not a set. To get an empty set, use set().
Since a set has no order, you cannot get an item by position. Small whole numbers often print in sorted order, but never rely on that.
A for loop visits every item. The in word checks whether a value is present.
| Method | What it does | Returns |
|---|---|---|
| add(item) | Adds one item | None |
| update(iterable) | Adds many items at once | None |
| remove(item) | Removes an item, error if missing | None |
| discard(item) | Removes an item, no error if missing | None |
| pop() | Removes and returns an arbitrary item | The item |
| clear() | Removes every item | None |
| copy() | Makes a copy | A new set |
| union(other) | All items from both | A new set |
| intersection(other) | Items found in both | A new set |
| difference(other) | Items in the first but not the second | A new set |
| symmetric_difference(other) | Items in one but not both | A new set |
| issubset(other) | Is every item also in the other set? | True or False |
| issuperset(other) | Does it contain every item of the other? | True or False |
| isdisjoint(other) | Do the two share nothing? | True or False |
add puts in one item. Adding something that is already there changes nothing.
update adds many items from an iterable, such as a list, a tuple, or another set. Put the numbers in a list. Separate numbers, as in x.update(8, 11), fail.
You can pass more than one iterable, as the second line shows.
len counts the items. It is a built in function, not a set method.
Both take out an item. The difference is what happens when the item is missing. remove raises a KeyError, and discard stays silent.
The discard call on 99 did nothing and raised no error. The remove call on 99 raised the KeyError.
pop removes one item and hands it back. A set has no order, so it does not remove from the end. It removes an arbitrary item, and you cannot choose which one.
An empty set prints as set(), not as {}, to avoid mixing it up with an empty dictionary.
There is no x.delete() method. del is a statement, not a method. It removes the name itself, so using the set afterwards fails.
The copy is independent, so adding to b did not touch a.
Take two sets, x and y. Each operation has a method and a shortcut operator.
| Operation | Operator | In plain words |
|---|---|---|
| union | | | Everything in either set |
| intersection | & | Only what both sets have |
| difference | – | In the first set but not in the second |
| symmetric difference | ^ | In one set but not in both |
Think of the maths club and the science club. The union is everyone in either club. The intersection is the students who joined both. The difference is the students in maths who are not in science.
A set is a subset of another if every one of its items is also in the other. The other set is then a superset of it. The direction matters. Here x has 6 items and small has 3 of them:
| Question | Answer | Why |
|---|---|---|
| Is x a subset of small? | False | x has items that small lacks |
| Is small a subset of x? | True | Every item of small is in x |
| Is x a superset of small? | True | x contains all of small |
| Is small a superset of x? | False | small is missing items of x |
isdisjoint is True when two sets share no items.
A set can only hold hashable items, which in practice means items that cannot change. Numbers, text and tuples work. A list does not.
The last line shows a tuple inside a set, and that two sets with the same items are equal in any order.
This is the most common real use of a set. Turn a list into a set and back.
The first line removes the duplicates but loses the original order, so the output is sorted here. If order matters, use the second form. A dictionary remembers the order of first appearance, and it gave Asha, Ravi, Meena, the order they came in.
A set comprehension builds a set in one line. Ten numbers gave only three remainders, because the repeats collapsed.
A frozenset is a set that cannot be changed. Because it is unchangeable, it can be used as a dictionary key or placed inside another set.
| Type | Written as | Ordered | Duplicates | Best for |
|---|---|---|---|---|
| List | [1, 2, 3] | Yes | Allowed | A collection that grows or changes |
| Tuple | (1, 2, 3) | Yes | Allowed | A fixed group of values |
| Set | {1, 2, 3} | No | Not allowed | Unique items and group comparisons |
| Dictionary | {“a”: 1} | Yes | Keys must be unique | Looking up values by key |
set().discard.A.issubset(B) asks whether A fits inside B.dict.fromkeys instead.del, or clear to empty it.The program below holds every example in this tutorial. Copy it into a file, run it, and compare your output with what you see above.
Sets do one job very well: they keep things unique and make group comparisons easy. Run the examples, change the numbers, and watch how union, intersection and difference behave. When they feel natural, you will reach for a set whenever you hear the words unique, duplicate or overlap. Then move on to the other Python data types to see how all five fit together.