Menu

Python Sets: Unique Items, Union, Intersection, and Difference

When to reach for a Python set — uniqueness, fast in checks, and the math-style operations (union, intersection, difference) that make sets powerful.

A Set Is an Unordered Bag of Unique Items

Where lists and tuples care about order, sets don't. Where lists let duplicates stack up, sets silently drop them. The set is the right data structure when you have two specific needs:

  1. You want unique items, full stop.
  2. You want to check membership fast.

Write one with curly braces:

main.py
Output
Click Run to see the output here.

The duplicate "red" and "green" in the second set just disappear. That's not an error — it's the whole point.

Creating Sets

The two ways you'll actually use:

main.py
Output
Click Run to see the output here.

That last point catches everyone once: {} creates an empty dictionary, not an empty set. The syntactic ambiguity had to land on one of them, and dicts won.

Adding and Removing

main.py
Output
Click Run to see the output here.

remove vs discard is the key distinction: remove insists the item be present; discard doesn't care. Pick based on whether the absence of the item should be an error.

Fast Membership

This is where sets really earn their place. x in some_set runs in constant time, no matter how big the set is. x in some_list has to walk the list, which gets slow when the list is large.

main.py
Output
Click Run to see the output here.

Rule of thumb: any time you find yourself writing if x in some_list inside a loop, and the list has more than a few dozen items, convert the list to a set first.

Set Math

This is where sets get genuinely fun. You can combine them with operators that mirror mathematical set operations:

main.py
Output
Click Run to see the output here.

Each operator has a method form too (.union(), .intersection(), .difference(), .symmetric_difference()). The operators are more compact; the method forms accept any iterable, not just another set.

Deduplicating a List

One of the most common uses of sets, even outside of "set logic":

main.py
Output
Click Run to see the output here.

One line, dupes gone. One thing to note: the order is not preserved. If you need uniqueness and the original order, use dict.fromkeys() instead:

main.py
Output
Click Run to see the output here.

Dicts preserve insertion order in modern Python, and dict.fromkeys builds one using the iterable's items as keys — effectively an ordered set.

Subsets and Supersets

Checking whether one set is contained in another:

main.py
Output
Click Run to see the output here.

These come up in things like permissions checks ("does this user have all required roles?").

What Can Go in a Set

Only hashable items. That's a technical term — in practice, it means:

  • Immutable things are hashable: numbers, strings, tuples of hashables, frozensets.
  • Mutable things aren't: lists, dicts, other sets can't be set members.
main.py
Output
Click Run to see the output here.

If you need a set of sets, use frozenset — it's an immutable version of set.

Iteration Is Unordered

Looping over a set gives you items in no guaranteed order:

main.py
Output
Click Run to see the output here.

Run that a few times and you'll see the order vary. If order matters, a set isn't the right structure — sort the contents when you need them, or use a list.

When Not to Use a Set

If any of these are true, a list or dict is probably a better fit:

  • You care about order.
  • You need to store duplicates.
  • Each item has associated data (use a dict with the item as the key).

Moving On

Sets handle uniqueness and membership. Dictionaries — coming next — handle the broader "lookup a value by a key" pattern, which is probably the most useful non-list data structure in Python.

Frequently Asked Questions

What is a Python set?

A set is an unordered collection of unique items. Sets use curly braces with no key-value pairs: colors = {'red', 'green', 'blue'}. Adding the same value twice has no effect — duplicates are silently dropped.

When should I use a set instead of a list?

Use a set when you care about uniqueness or when you'll be doing many membership checks (x in collection). Sets remove duplicates automatically and check membership in constant time, which for large collections is a huge speedup over lists.

How do I create an empty set in Python?

Use set(), not {} — curly braces with nothing inside create an empty dictionary, not an empty set. Once you have a set, you can add items with .add(value).

Learn to code with Coddy

GET STARTED