Sets

A set is Python's collection of unique items. Drop duplicates in and they silently disappear; ask "is this here?" and you get an answer almost instantly. Sets are the right tool whenever uniqueness or fast membership matters.

Learn Sets in our free Python course — a beginner-friendly interactive lesson with runnable examples, a practice exercise and a quick recall.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

They also bring the elegant math of unions, intersections, and differences — perfect for comparing two groups of things.

What You'll Learn in This Lesson

1 Creating a Set

Write a set with curly braces and commas, or build one from any iterable with set() . Duplicates are removed on the spot:

⚡ The Superpower: Instant Membership Tests

The single biggest reason to use a set is speed. Checking x in some_set is roughly constant time no matter how big the set is, while the same check on a list scans every element:

If your program repeatedly asks "have I seen this before?" or "is this in the allowed list?", store the values in a set. For a large collection the difference is the gap between instant and noticeably slow.

2 Adding and Removing Items

Sets are mutable — you can change their contents (even though the elements must be immutable):

3 Set Math: Union, Intersection, Difference

This is where sets shine. Comparing two groups of things becomes a single, readable operation:

🧰 The Classic De-duplication Pattern

One of the most common real uses of a set is to remove duplicates from a list in a single line:

list(set(items)) is the quick way, but it scrambles order. The "seen" pattern is the standard idiom when you need uniqueness and the original ordering.

🧩 Reorder Challenge

These lines should find how many tags two articles share. Reorder them so the final output is Shared tags: 2 .

Why: both sets a (B) and b (D) must exist before the intersection a & b (A) can run. The print (C) reports the size of that result. The two common tags are web and api .

🧠 Quick Recall

3 — sets discard duplicates, leaving only {1, 2, 3} , which has length 3.

{2, 3} — the & operator returns the intersection: only the elements present in both sets.

<class 'dict'> — empty curly braces make a dictionary, not a set. Use set() for an empty set.

📋 Set Operations Cheat Sheet

Operation

Meaning

Example

x in s

Membership (fast)

2 in {1,2} → True

s.add(x)

Insert one item

s.add(5)

a | b

Union

All items from both

a & b

Intersection

Items in both

a - b

Difference

In a, not in b

list(set(x))

De-duplicate

Drop repeats

Frequently Asked Questions

🎯 Mini Challenge: Find Common Friends

Given two people's friend lists, find mutual friends and friend suggestions.

Lesson complete — sets are in your toolkit!

You can create sets, de-duplicate data, run instant membership tests, add and remove items, and use union, intersection, and difference to compare groups. Remember: set() for empty, not {} .

🚀 Up next: Tuples — the immutable, fixed sibling of the list.

Practice quiz

What is the defining feature of a Python set?

  • It keeps items sorted
  • It allows duplicates
  • It stores only unique items
  • It is indexed by position

Answer: It stores only unique items. A set is an unordered collection that automatically removes duplicates.

What does len({1, 2, 2, 3, 3, 3}) return?

  • 3
  • 6
  • 1
  • 2

Answer: 3. Duplicates are discarded, leaving {1, 2, 3}, which has length 3.

How do you create an EMPTY set?

  • {}

{} creates an empty dict; use set() for an empty set.

What does {1, 2, 3} & {2, 3, 4} produce?

  • {1, 2, 3, 4}
  • {2, 3}
  • {1, 4}
  • {1}

Answer: {2, 3}. The & operator is intersection: only items in both sets, {2, 3}.

Which operator gives the UNION of two sets?

  • |
  • &
  • -
  • ^

Answer: |. The | operator returns the union — all items from both sets.

What does a - b compute for two sets?

  • Items in both
  • Items in either
  • Items in a but not b
  • Items in neither

Answer: Items in a but not b. The - operator is the difference: items in a that aren't in b.

Why is 'x in my_set' faster than 'x in my_list' for large data?

  • Sets are sorted
  • Sets use a hash table (O(1) lookup)
  • Lists are immutable
  • Sets are smaller

Answer: Sets use a hash table (O(1) lookup). Sets are hash-table backed, giving roughly O(1) membership tests vs O(n) for lists.

What's the difference between remove() and discard()?

  • No difference
  • discard() raises KeyError, remove() does not
  • remove() adds items
  • remove() raises KeyError if missing, discard() does not

Answer: remove() raises KeyError if missing, discard() does not. remove() raises a KeyError when the item is absent; discard() is silent.

What is type({}) in Python?

  • set
  • dict
  • list
  • tuple

Answer: dict. Empty curly braces create a dictionary, not a set.

Which one-liner removes duplicates from a list called items?

  • sorted(items)
  • items.unique()
  • list(set(items))
  • set.remove(items)

Answer: list(set(items)). list(set(items)) dedupes by converting to a set and back (order not preserved).

Continue this course