Python Set Methods

About This Index
This page is the Python Set Methods index for the tutorial track. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. A set has no fixed order, so every example prints sorted() results, and the output you see is the output you get.
Learn creating sets, the set rules, set math, and the classic mistakes on the Set hub. Use this page when you need a specific method such as add(), discard(), or union().
Every method below belongs to the built-in set type, so no import is needed. Nine of them change the set in place; of those, only pop() hands something back, and the rest return None. The other eight leave the set alone and return a new set or True / False. The Returns column tells you which is which.
How to Call a Set Method
Write the set, a dot, the method name, and the arguments. Methods that change the set return None, so call them on their own line; union(), issubset(), and the other methods that answer a question give you a value to use:
tags = {"python", "sets"}
tags.add("loops") # add one item
tags.update(["sql", "git"]) # add many items
tags.discard("git") # remove; never raises
print(sorted(tags)) # ['loops', 'python', 'sets', 'sql']
more = tags.union({"c"}) # a new set; tags is unchanged
print(len(tags), len(more)) # 4 5
print(tags.issubset(more)) # True Python Set Methods Table
All method guides in this Python tutorial series (17 tutorials), grouped by purpose.
Add items
2 methodsPut one item into the set, or every item of one or more iterables, in place. Duplicates are always ignored.
Remove items
4 methodsDelete a chosen item (with or without an error for a missing one), take out an arbitrary item and get it back, or empty the whole set in place.
| Method | Returns | Description | Tutorial |
|---|---|---|---|
remove() | None | Delete an item from the set; raises KeyError if the item is missing. | Try it |
discard() | None | Delete an item if it is in the set; do nothing if it is not. | Try it |
pop() | the removed item | Remove and return an arbitrary item; raises KeyError if the set is empty. | Try it |
clear() | None | Remove every item from the set, in place. | Try it |
Combine sets into a new set
4 methodsMerge, overlap, subtract, or compare sides, and get a brand-new set while the originals stay as they are.
| Method | Returns | Description | Tutorial |
|---|---|---|---|
union() | set (new) | Return a new set with every item from the set and the other iterables. | Try it |
intersection() | set (new) | Return a new set with only the items found in the set and in every other iterable. | Try it |
difference() | set (new) | Return a new set with the items of the set that are not in any other iterable. | Try it |
symmetric_difference() | set (new) | Return a new set with the items that are in exactly one of the two sets. | Try it |
Change the set in place
3 methodsThe same set math as above, but the result is written back into the set itself and the method returns None.
| Method | Returns | Description | Tutorial |
|---|---|---|---|
intersection_update() | None | Keep only the items that also appear in every other iterable, in place. | Try it |
difference_update() | None | Remove every item found in the other iterables from the set, in place. | Try it |
symmetric_difference_update() | None | Keep the items that are in exactly one of the set and the other iterable, in place. | Try it |
Compare and copy
4 methodsAsk whether two sets overlap, contain each other, or make an independent copy of a set.
| Method | Returns | Description | Tutorial |
|---|---|---|---|
isdisjoint() | bool | Return True if the set and the other iterable share no items. | Try it |
issubset() | bool | Return True if every item of the set is also in the other iterable. | Try it |
issuperset() | bool | Return True if the set contains every item of the other iterable. | Try it |
copy() | set (new) | Return a new set with the same items. | Try it |
Set Operators and Idioms
These are not named methods, but you will use them next to the methods above in almost every program:
| Code | What it does |
|---|---|
x in s | True if x is in the set. Very fast, even for huge sets. |
len(s) | Number of items. |
a | b | New set with the items of both; like a.union(b), but both sides must be sets. |
a & b | New set with the items found in both; like a.intersection(b). |
a - b | New set with the items of a that are not in b; like a.difference(b). |
a ^ b | New set with the items that are in exactly one of them; like a.symmetric_difference(b). |
a <= b | True if every item of a is in b, like a.issubset(b). a < b also needs b to have extra items. |
a >= b | True if a contains every item of b, like a.issuperset(b). a > b also needs a to have extra items. |
a |= b | Add the items of b in place, like a.update(b), but b must be a set. |
a &= b | Keep only the shared items, in place, like a.intersection_update(b). |
a -= b | Remove the items of b, in place, like a.difference_update(b). |
a ^= b | Toggle the items of b, in place, like a.symmetric_difference_update(b). |
{x for x in items} | Set comprehension: build a set from any loop, dropping duplicates. |
sorted(s) | New sorted list of the items; the usual way to print a set in a fixed order. |
frozenset(s) | An unchangeable set, which can itself be an item of another set or a dict key. |
a = {1, 2, 3, 4}
b = {3, 4, 5}
print(sorted(a | b)) # [1, 2, 3, 4, 5]
print(sorted(a & b)) # [3, 4]
print(sorted(a - b)) # [1, 2]
print(sorted(a ^ b)) # [1, 2, 5]
print({3, 4} <= a, a < b) # True False
print(2 in a, len(a)) # True 4
a |= {9} # same as a.update({9})
print(sorted(a)) # [1, 2, 3, 4, 9] Pick the method form, such as union() or update(), when the other side is a list, tuple, or string, or when you pass several iterables, and the operator form when both sides are sets. Use intersection() or a & b to get a new set, and intersection_update() or a &= b to change a itself.
Frequently Asked Questions
Start with add()
Open the first guide in the set and work your way through the set methods.
16 people found this page helpful
