TetSharing
Python tutorial · 22 chapters
  1. Get started
  2. Syntax and comments
  3. Variables
  4. Numbers and math
  5. Strings
  6. f-strings and formatting
  7. Booleans and logic
  8. if, elif, else
  9. Lists
  10. Tuples
  11. Dictionaries
  12. Sets
  13. for loops and range()
  14. while, break, continue
  15. Functions
  16. Default and keyword arguments
  17. Lambda and sorting keys
  18. List comprehensions
  19. Errors and exceptions
  20. Modules and imports
  21. Reading and writing files
  22. Classes and objects

Final Python quiz
Cheat sheet: python string methods
Cheat sheet: python lists dicts

Chapter 12 of 22 · By Ethan Cole · Updated November 21, 2026

Python Sets: Unique Values, Union and Intersection

A set is an unordered collection of unique values. Adding something that is already present has no effect, which makes sets the quickest way to remove duplicates. Checking whether a value is in a set is also very fast, even with millions of items, because sets use the same hashing as dictionary keys.

Creating sets

Write a set with braces: {"python", "sql"}, or build one from any iterable with set(). In the example, "python" appears twice in the literal but the set holds it once, so len(tags) is 3. set("banana") gives the distinct letters of a word.

There is one trap: {} creates an empty dictionary, not an empty set. Use set() for that.

Set algebra

Sets support the operations you may remember from school, each available as an operator or a method:

  • a | b (or a.union(b)): everything in either set.
  • a & b (intersection): only what both have.
  • a - b (difference): what is in a but not in b.
  • a ^ b (symmetric_difference): what is in exactly one of them.
Python 3 · editable

Expected output

3 True
{1, 2, 3, 4, 5}
{3, 4}
{1, 2}
{1, 2, 5}
['a', 'b', 'n']
<class 'set'> <class 'dict'>

Sets have no order, so you cannot index them and their printed order is not something to rely on. When you need a stable order for display, use sorted(), which returns a list. Small integer sets happen to print in ascending order, but that is a detail of how CPython stores them.

Changing a set

add(x) inserts one value, update(iterable) inserts many, discard(x) removes a value if present, and remove(x) does the same but raises KeyError when it is missing. Like dictionary keys, set members must be hashable, so you can put strings, numbers and tuples in a set, but not lists. If you need an immutable set, for example as a dictionary key, use frozenset.

Common mistakes

  • Writing {} for an empty set.
  • Expecting a set to keep insertion order or allow s[0].
  • Converting to a set to deduplicate and then being surprised that the original order is lost. When order matters, list(dict.fromkeys(items)) keeps the first occurrence of each value.

Summary

  • Sets hold unique, hashable values with no order.
  • |, &, - and ^ compute union, intersection, difference and symmetric difference.
  • set() is the empty set; {} is an empty dict.

Quiz: Sets

1. What is len({1, 1, 2, 3, 3})?

Duplicates are dropped, leaving 1, 2 and 3.

2. What is {1, 2, 3} & {2, 3, 4}?

& keeps only values found in both sets.

3. How do you create an empty set?

{} is an empty dictionary; set() is the empty set.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.