Technology Sets | Python Tutorial

Learn with diagrams, code, systems and practical examples.

</>
TutorialsPythonSets
Data Structures · Chapter 12

Sets

Track unique values and perform membership operations.

Why this matters

Sets are excellent when the question is whether a value has been seen or belongs to a group.

Start with the idea

A set stores unique hashable values. Membership checks are usually efficient, and set operations can combine or compare groups.

IdeaUnderstand the purpose
CodeRun the smallest example
PracticeChange it yourself
SubjectVision learning pattern: understand the idea before memorizing syntax.

Small working example

Run this example first. Do not change several things at once; confirm the basic behavior, then experiment.

Python
seen = {"a", "b"}
seen.add("a")
seen.add("c")
print("b" in seen)
print(len(seen))
Expected output
True
3
What to notice

The example is intentionally small. Focus on the chapter’s main idea before adding extra syntax or framework code.

Common beginner mistake

Mistake

Using a list for repeated membership checks when order and duplicates do not matter.

Better approach

Use a set when uniqueness and membership are the real requirements.

Quick recap

  • Sets keep unique values.
  • Membership is a core set operation.
  • Sets do not model positional order.

Try it yourself

These are deliberately small. If you can complete them without copying the example, you are ready to continue.

  1. Remove duplicates from sample input with a set.
  2. Find values common to two sets.