Python Sets: Unique Items, Union and Intersection - Python with AI tutorial chapter 15
Python

Python Sets: Unique Items, Union and Intersection

Python with AI Tutorial · Chapter 15 of 48

Python sets are unordered collections of unique items, written in curly braces such as {"North", "South", "East"}. Add a value that is already present and nothing happens, which makes sets the fastest way to remove duplicates, test membership and compare two groups. This chapter covers creating Python sets, adding and removing items, and the union, intersection and difference operations.

Creating a set

List the items in curly braces, or pass any iterable to set(). Duplicates disappear immediately. Sets have no index and no guaranteed order, so when you need predictable output, print sorted(my_set), which returns a list.

customer_ids = {104, 219, 104, 355, 219}
print(customer_ids)
print(len(customer_ids))

regions = {"North", "South", "East", "North"}
print(len(regions))
print(sorted(regions))
Output:
{104, 219, 355}
3
3
['East', 'North', 'South']

Set items must be hashable, which in practice means immutable: numbers, strings, booleans and tuples are fine, but lists and dictionaries are not. Trying to add a list raises TypeError: unhashable type: 'list'.

Removing duplicates from a list

The most common use of a set is a one-line de-duplication. Convert the list to a set, then back to a list if you need indexing again. Note that {} creates an empty dictionary, so an empty set must be written set().

orders = ["Acme", "Bright Co", "Acme", "Delta", "Bright Co"]
unique_customers = set(orders)

print(len(unique_customers))
print(sorted(unique_customers))

empty = set()
print(type({}), type(empty))
Output:
3
['Acme', 'Bright Co', 'Delta']
<class 'dict'> <class 'set'>

If the original order matters, use list(dict.fromkeys(orders)) instead; dictionaries remember insertion order and also reject duplicate keys.

Adding and removing items

add() inserts one item and update() inserts every item from another collection. remove() deletes an item and raises KeyError if it is missing, while discard() deletes it quietly whether or not it exists. Membership testing with in is extremely fast on sets, even with millions of items.

skills = {"excel", "sql"}
skills.add("python")
skills.update(["power bi", "excel"])
print(sorted(skills))

skills.remove("sql")
skills.discard("tableau")     # not present, no error
print(sorted(skills))
print("python" in skills)
Output:
['excel', 'power bi', 'python', 'sql']
['excel', 'power bi', 'python']
True

Sets also have pop(), which removes and returns an arbitrary item, and clear(), which empties the set. Because there is no order, pop() takes no index argument.

Union, intersection, difference

Set algebra is where sets earn their keep. Given two groups of people, you can ask who is in either, who is in both, who is only in the first, and who is in exactly one, each with a single operator.

excel_users = {"Anita", "Rahul", "Meera", "Vikram"}
python_users = {"Meera", "Vikram", "Priya"}

print(sorted(excel_users | python_users))   # union
print(sorted(excel_users & python_users))   # intersection
print(sorted(excel_users - python_users))   # difference
print(sorted(excel_users ^ python_users))   # symmetric difference
Output:
['Anita', 'Meera', 'Priya', 'Rahul', 'Vikram']
['Meera', 'Vikram']
['Anita', 'Rahul']
['Anita', 'Priya', 'Rahul']
Operation Operator Method Result for A = {1, 2, 3}, B = {3, 4}
Union (in A or B) A | B A.union(B) {1, 2, 3, 4}
Intersection (in both) A & B A.intersection(B) {3}
Difference (in A, not B) A - B A.difference(B) {1, 2}
Symmetric difference (in exactly one) A ^ B A.symmetric_difference(B) {1, 2, 4}
Subset A <= B A.issubset(B) False
Superset A >= B A.issuperset(B) False
No common items A.isdisjoint(B) False

The operators require both sides to be sets. The method forms are more forgiving and accept any iterable, so excel_users.union(["Sanjay"]) works while excel_users | ["Sanjay"] raises TypeError.

Try it with AI

Give the assistant two real-world lists and ask it to answer business questions with set operations, naming the operation each time.

I have two Python lists. Last quarter's customers: ["Acme", "Bright Co", "Delta", "Evergreen", "Acme"]. This quarter's customers: ["Bright Co", "Delta", "Falcon", "Globex", "Delta"]. Using Python sets, show me how to find: customers we kept, customers we lost, brand-new customers, and everyone who bought in either quarter. For each answer name the set operation used (union, intersection, difference or symmetric difference), print the result sorted, and explain why a set is better than nested loops here.

Subset and superset tests

These comparisons answer questions such as “has everyone on the project completed the training?” without writing a loop.

all_staff = {"Anita", "Rahul", "Meera", "Vikram", "Priya"}
trained = {"Meera", "Vikram"}

print(trained.issubset(all_staff))
print(all_staff.issuperset(trained))
print(trained.isdisjoint({"Anita"}))
print(trained <= all_staff)
Output:
True
True
True
True

In-place updates

Each operator has an augmented form that changes the left-hand set instead of building a new one. -= removes a group of items and &= keeps only the items that also appear in another set.

inventory = {"laptop", "mouse", "monitor"}
sold = {"mouse", "monitor"}
inventory -= sold
print(inventory)

allowed = {"csv", "xlsx", "json"}
uploaded = {"csv", "exe", "xlsx"}
allowed &= uploaded
print(sorted(allowed))
Output:
{'laptop'}
['csv', 'xlsx']

Finding duplicates with a set

Because membership checks are so fast, a set of items already seen is the standard way to detect duplicates in one pass through the data.

emails = ["a@x.com", "b@x.com", "a@x.com", "c@x.com", "b@x.com"]
seen = set()
duplicates = set()

for email in emails:
    if email in seen:
        duplicates.add(email)
    seen.add(email)

print(sorted(duplicates))
Output:
['a@x.com', 'b@x.com']

The same job with a list for seen would work, but each in check would scan the whole list, so the run time grows with the square of the data size. With a set it stays roughly proportional to the number of items.

Set comprehensions and frozenset

A set comprehension looks like a list comprehension with curly braces and produces a set directly. frozenset is an immutable set: it cannot be changed after creation, so it can be used as a dictionary key or stored inside another set.

amounts = [120, 85, 120, 300, 85, 47]
big = {a for a in amounts if a >= 100}
print(big)

ADMIN_ROLES = frozenset({"admin", "editor"})
print("admin" in ADMIN_ROLES)

access = {frozenset({"admin"}): "full access"}
print(access[frozenset({"admin"})])
Output:
{120, 300}
True
full access
Common mistake: relying on the order a set prints in. Small integers often appear sorted because of how hashing works, but strings are deliberately shuffled between runs, so {"b", "a"} may print either way round. Never index a set, never assume its first item, and sort it whenever the order matters to a reader.
Try it with AI

Ask the assistant to benchmark membership testing in a list versus a set so you can see the difference for yourself.

Write a Python 3.12 script that builds a list and a set each containing the integers 0 to 999,999, then uses the timeit module to measure how long it takes to check whether 20 random numbers are present in the list versus the set. Print both timings and the speed-up factor, and explain in two sentences why the set is faster and when a list would still be the right choice.

Common mistakes

  • Writing {} for an empty set. That is an empty dictionary; use set().
  • Trying to index a set with my_set[0], which raises TypeError because sets have no order.
  • Adding a list or dictionary to a set. Convert the list to a tuple first.
  • Using remove() on an item that might be absent. Use discard() or check with in.
  • Mixing a set and a list with an operator such as |. Use the method form or convert the list with set().

Exercise

Last month’s customers were {"Acme", "Bright Co", "Delta", "Evergreen"} and this month’s are {"Bright Co", "Delta", "Falcon", "Globex"}. Print the new customers, the lost customers and the retained customers, each sorted, and then the retention rate as a percentage of last month’s customers.

Show answer
last_month = {"Acme", "Bright Co", "Delta", "Evergreen"}
this_month = {"Bright Co", "Delta", "Falcon", "Globex"}

print("New:", sorted(this_month - last_month))
print("Lost:", sorted(last_month - this_month))
print("Retained:", sorted(last_month & this_month))
print(f"Retention: {len(last_month & this_month) / len(last_month):.0%}")
Output:
New: ['Falcon', 'Globex']
Lost: ['Acme', 'Evergreen']
Retained: ['Bright Co', 'Delta']
Retention: 50%

Two differences and one intersection answer all three questions; no loops are needed.

Related chapters

FAQ

Are Python sets ordered?

No. A set has no positions, so you cannot index or slice it and the order it prints in is not guaranteed. Use sorted() when you need a predictable order, or a list if order is part of the data.

How do I remove duplicates from a list in Python?

Convert it to a set and back: list(set(items)). If you also need to keep the original order, use list(dict.fromkeys(items)), which removes duplicates while preserving first appearances.

What is the difference between remove() and discard()?

Both delete one item from a set. remove() raises KeyError if the item is not present, while discard() does nothing in that case, so discard() is safer when you are not sure the item exists.

Working with spreadsheets too? Ready-made Excel, Google Sheets and Power BI templates are at NextGenTemplates.com.

Chapter 15 of 48 · Python with AI: all 48 chapters

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com