✦ Python Foundations Collections module

Concept 10 · Sets

Keep values unique.
Do not rely on order.

A set keeps unique values when membership matters more than position. Use it to remove duplicates and ask whether a value is present.

01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

A set keeps unique values when membership matters more than position.

You already know that lists can grow and change, and tuples keep fixed related values together. A set solves a different problem: it keeps each value once. Use a set when duplicates should disappear or when the useful question is “Is this value here?”—not “Which value is at position 0?”

tags = ["python", "django", "python", "ai"]
unique_tags = set(tags)

print(unique_tags)
print(len(unique_tags))

A list can contain the same value more than once. set(tags) keeps only unique values, so this set has three elements: "python", "django", and "ai". Do not rely on the displayed order of a set; the important result is that duplicates disappear.

A set literal uses braces: skills = {"python", "django", "ai"}. Writing "python" twice still creates only one "python" element. len(skills) counts the elements that actually remain after duplicates disappear.

↳

Do not choose a set for positions: A list or tuple is usually better when order or position matters. Do not use skills[0] with a set, and do not write program logic that depends on which element appears “first” when a set is displayed or iterated.

Membership asks a natural set question
skills = {"python", "django", "ai"}

print("django" in skills)
print("java" in skills)

value in set produces a Boolean result. Here "django" in skills is True, while "java" in skills is False. This connects sets to the bool values, operators, and conditions you already know.

Add or discard one value
skills = {"python", "django", "ai"}

skills.add("data")
skills.add("python")
skills.discard("django")

print(skills)

add() adds one value. Adding a value that is already present does not create a duplicate. discard() removes a value when it exists and does not cause a problem when it is absent.

Union, intersection, and difference
a = {1, 2, 3}
b = {3, 4, 5}

print(a | b)
print(a & b)
print(a - b)

a | b is the union: all unique values that appear in either set, {1, 2, 3, 4, 5}. a & b is the intersection: values shared by both sets, {3}. a - b is the difference: values in a but not in b, {1, 2}.

Create an empty set with set()
empty_set = set()
empty_dictionary = {}

{} is not an empty set in Python; it creates an empty dictionary. Use set() when you need an empty set. Dictionaries have their own lesson later.

Trace a set as its state changes
skills = {"python", "django", "python"}

skills.add("ai")
skills.add("python")

print(len(skills))
print("django" in skills)

Pause before running it. The duplicate "python" does not make another element. "ai" is added, and adding "python" again changes nothing. The final set has three unique elements, so len(skills) is 3; membership for "django" is True.

Choose a collection by what matters

  1. Choose a set for uniqueness and membershipUse a set for unique visitors, tags, permissions, selected categories, or shared interests when duplicates should disappear.
  2. Choose a list when sequence mattersUse a list when order or position matters, duplicates may matter, or items change in a sequence.
  3. Choose a tuple for a fixed related groupUse a tuple when fixed related values belong together and their positions have meaning.

Common beginner mistakes

  • Expecting duplicates to remain{"python", "python"} does not represent two separate "python" values; a set keeps it once.
  • Trying to index a setskills[0] does not work because a set does not provide position-based indexing like a list or tuple.
  • Treating {} as an empty setempty = {} creates a dictionary. Use empty = set() for an empty set.
  • Relying on set orderDo not make program logic depend on which set element is displayed or visited first. Use a list or tuple when that order is part of the meaning.

Python Playground

Loading Python runtime…

Before running the code, predict how many unique values the set contains, the membership result, and what changes after add(). Then try adding a value that is already present.

sets.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Read each operation before choosing an answer.

a = {1, 2, 3}
b = {3, 4, 5}

print(len(a & b))
print(len(a | b))
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.