Python Sets Explained: No Duplicates, Fast Membership Checks

The one structure whose whole job is 'no repeats' — and the one honest limit that comes with it.

  • Lesson 3 of 4
  • PRO
  • Ages 10+
  • 3 hands-on levels
See what PRO unlocksA one-time $49.99 purchase. Topics 1–3 are free.

The dragon explains

Sets: no duplicates, fast membership

  1. The dragon: Lists, dicts, and now one more: a set. Its whole job is simple — no duplicates, ever, and fast 'is this in here' checks.

  2. The dragon: set(a_list) throws away every duplicate automatically. 6 visits, but only 3 different rooms.

    example.pyPython 3
    visited = ['cave', 'forest', 'cave', 'lake', 'forest', 'cave']unique_rooms = set(visited)print(len(unique_rooms))
    Output3
  3. The dragon: You can also write a set directly with curly braces — {} — same symbol as a dict, but with values only, no keys.

    example.pyPython 3
    allowed = {'sword', 'shield', 'bow'}print('axe' in allowed)
    OutputFalse
  4. The dragon: One real limit, worth knowing rather than discovering the hard way: a set has NO guaranteed order.

  5. The dragon: Printing a set directly can show its items in any order — not wrong, just unpredictable. sorted() fixes that when you actually need a real order.

    example.pyPython 3
    tags = ['fire', 'ice', 'fire', 'poison']unique_tags = set(tags)print(sorted(unique_tags))
    Output['fire', 'ice', 'poison']
  6. The dragon: Rule of thumb: reach for a set when the question is 'how many different things' or 'is this one of them' — and convert to a sorted list the moment order matters.

Quick check

Check your understanding

Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.

  1. Question 1 of 2

    visited = ['cave', 'forest', 'cave', 'lake']. What does len(set(visited)) give you?

    question.pyPython 3
    print(len(set(visited)))
    • 3 — cave, forest, and lake are the only DIFFERENT roomsCorrect
    • 4 — the same as len(visited), since converting to a set doesn't remove anything

    Why: 3. set(visited) collapses the two 'cave' entries into one — a set can never contain the same value twice. cave, forest, and lake are the only genuinely different rooms, even though the original list mentions 4 visits.

  2. Question 2 of 2

    unique_tags = set(['fire', 'ice', 'poison']). What's true about print(unique_tags)'s order?

    • The order isn't guaranteed — it might not match the order the items were added inCorrect
    • It always prints in the same order they were added, just like a list

    Why: Not guaranteed. A set is built for fast membership checks, not for remembering insertion order the way a list does — printing it directly can come out in a different order than you added things. sorted() is the fix whenever the order actually matters.

In the game

What you'll practice

Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.

  1. 1

    GuidedYour first try

    visited = ['cave', 'forest', 'cave', 'lake', 'forest', 'cave'] — the dragon revisited some rooms. How many DIFFERENT rooms did it actually visit, not counting repeats? Turn the list into a set with set(visited), then print how many items it has.

  2. +2

    PracticeTwo more, on your own

This lesson is part of PRO.

PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.

Unlock this lesson with PRO

Stuck three times? The game offers the answer — unasked.