Himanshu SharmaSoftware engineer
Let’s talk

Python Tutorial · Lesson 28 of 70

Python sets: unique items and set operations

Learn Python sets for removing duplicates and fast membership tests, plus union, intersection and difference, with output that does not depend on set order.

13 minCollectionsUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonCreate sets, remove duplicates from data, test membership, and combine sets with union, intersection and difference.

After this lesson you can remove duplicates from data, ask quickly whether something is present, and compare two groups of items with set operations. A set is a collection of unique values, much like the sets you may remember from maths.

Creating a set

Write a set with curly braces. Python quietly throws away repeats:

python
colours = {"red", "green", "blue", "red", "green"}
print(len(colours))
print(sorted(colours))
result
3
['blue', 'green', 'red']

We print sorted(colours) rather than colours on purpose. A set does not promise any particular order, and the order you see can differ between runs, so you should never depend on it. When you need to show a set in a stable way, sort it, which gives you a list.

For small whole numbers, the order tends to look sorted, but that is an implementation detail and not something to rely on:

python
numbers = {3, 1, 2, 3, 1}
print(numbers)
print(len(numbers))
result
{1, 2, 3}
3

To make an empty set, call set(). Writing {} makes an empty dictionary, which is a different type that you will meet shortly.

python
empty_set = set()
empty_dict = {}
print(type(empty_set))
print(type(empty_dict))
result
<class 'set'>
<class 'dict'>

set() also converts other collections. That is the quickest way to remove duplicates:

python
visits = ["Asha", "Ben", "Asha", "Chloe", "Ben", "Asha"]
unique_visitors = set(visits)
print(len(visits), len(unique_visitors))
print(sorted(unique_visitors))
result
6 3
['Asha', 'Ben', 'Chloe']

If you also need the original order kept, a set alone will not do; the dictionary lesson shows a trick for that.

No indexes, but fast membership

Because a set has no order, there is no position to ask for:

python
colours[0]
result
TypeError: 'set' object is not subscriptable

What a set is very good at is answering "is this in here?". Testing with in is fast even when the set is large, much faster than scanning a long list:

python
allowed = {"admin", "editor", "viewer"}
print("editor" in allowed)
print("guest" in allowed)
print("guest" not in allowed)
result
True
False
True

You can still loop over a set with for, but remember the order is not defined. Items that go into a set must be hashable, which in practice means immutable values such as numbers, strings and tuples. A list cannot be a set member:

python
bad = {[1, 2], [3, 4]}
result
TypeError: unhashable type: 'list'

Adding and removing

Sets are mutable. Add one item with add, many with update:

python
tags = {"python", "sql"}
tags.add("git")
tags.add("python")
tags.update(["css", "html"])
print(sorted(tags))
result
['css', 'git', 'html', 'python', 'sql']

To remove, remove raises a KeyError if the item is missing, while discard stays quiet:

python
tags.remove("css")
tags.discard("not-there")
print(sorted(tags))
result
['git', 'html', 'python', 'sql']

Comparing sets

The real power of sets is comparing groups. Imagine two classes and the students who joined each:

python
maths = {"Asha", "Ben", "Chloe", "Daniel"}
art = {"Chloe", "Daniel", "Elena"}
print(sorted(maths | art))
print(sorted(maths & art))
print(sorted(maths - art))
print(sorted(art - maths))
print(sorted(maths ^ art))
result
['Asha', 'Ben', 'Chloe', 'Daniel', 'Elena']
['Chloe', 'Daniel']
['Asha', 'Ben']
['Elena']
['Asha', 'Ben', 'Elena']

Read them as follows. | is the union, everyone in either class. & is the intersection, only students in both. - is the difference, the first set without anything from the second, so order matters. ^ is the symmetric difference, those in exactly one of the two classes.

Each operator also has a method form, such as maths.union(art) and maths.intersection(art). The methods accept any collection, not just sets. Testing relationships is just as easy:

python
small = {1, 2}
big = {1, 2, 3}
print(small <= big)
print(big >= small)
print(small.isdisjoint({7, 8}))
result
True
True
True

<= asks whether every item of the left set is in the right one, and isdisjoint asks whether the two share nothing.

Common mistakes

  • Using {} for an empty set. It is a dictionary. Write set().
  • Relying on the printed order. It is not guaranteed. Use sorted() whenever the order matters to you.
  • Indexing or slicing a set. There are no positions. Convert to a list or sorted list first if you need one.
  • Putting lists or other mutable items in a set. Use tuples instead.
  • Mixing up remove and discard. remove fails when the item is missing; discard does not.

Exercise

Two shops stock these items: shop A has "pen", "ruler", "glue", "paper" and shop B has "paper", "tape", "pen". Print, in sorted order, the items both shops sell, the items only shop A sells, and every item sold by either shop.

Show one possible answer
python
shop_a = {"pen", "ruler", "glue", "paper"}
shop_b = {"paper", "tape", "pen"}
print(sorted(shop_a & shop_b))
print(sorted(shop_a - shop_b))
print(sorted(shop_a | shop_b))
result
['paper', 'pen']
['glue', 'ruler']
['glue', 'paper', 'pen', 'ruler', 'tape']

Previous: Tuples. Next: Dictionaries.

Key points

  • A set holds unique items with no duplicates. Build one with curly braces, or with set() for an empty one, since {} is an empty dictionary.
  • The order of a set is not guaranteed and sets cannot be indexed. Use sorted(my_set) when you need a stable order.
  • Membership tests with in are very fast on sets, which makes them a good fit for lookups.
  • Union (|), intersection (&), difference (-) and symmetric difference (^) answer questions such as who is in both groups.

Check yourself

1. What does {} create?
2. What is len(set([4, 4, 4, 9]))?
3. Which operator gives the items that are in both set a and set b?
4. Why can you not write my_set[0]?