Python · Stage 5 – Grouping Data · Lesson 32 of 50

Sets and Unique Values

Store unique values when order and duplicates do not matter.

tags = {"python", "beginner", "python"}
Line-by-line explanation
  1. Curly braces contain the set values.
  2. Python appears twice in the code.
  3. The set stores it only once.
tags.add("coding")
Line-by-line explanation
  1. The add method belongs to the set.
  2. Coding is inserted if it is not already present.

Sets are especially useful for fast membership checks and removing duplicates. They do not promise list-style position order.

Tiny Practice

Create a set that stores red only once.

Starter template

colors = {"red", "red"}
print(colors)
Line-by-line explanation
  1. Two identical values are written.
  2. A set removes duplicates automatically.
Hint
  1. Curly braces already create the set.
  2. Run it without changing the duplicate.
  3. Observe the output.
Show Solution
colors = {"red", "red"}
print(colors)
Line-by-line explanation
  1. Both written items have the same value.
  2. The set keeps one red.
  3. The output contains red once.