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.
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.
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.
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}.
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.
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
- Choose a set for uniqueness and membershipUse a set for unique visitors, tags, permissions, selected categories, or shared interests when duplicates should disappear.
- Choose a list when sequence mattersUse a list when order or position matters, duplicates may matter, or items change in a sequence.
- 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 set
skills[0]does not work because a set does not provide position-based indexing like a list or tuple. - Treating {} as an empty set
empty = {}creates a dictionary. Useempty = 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.
