← Back to Blog

The Set: Unique Values, Nothing More

The problem...

You have a list of names. Some appear more than once.

attendees = ["Raven", "Wolf", "Raven", "Ghost", "Wolf", "Bull"]

You want only the unique names. No duplicates. No counting. Just the distinct values.

You could loop through and check manually. Or you could use the right tool.

The idea!

A set is a collection that stores only unique values.

Put anything in — duplicates are dropped automatically.

No index. No order. Just membership.

Making it real

attendees = {"Raven", "Wolf", "Ghost", "Bull"}
print(attendees)

Output → {'Bull', 'Ghost', 'Raven', 'Wolf'}

Curly braces — same as a dict. But no colons. No key-value pairs. Just values.

Duplicates are removed automatically

names = {"Raven", "Wolf", "Raven", "Ghost", "Wolf"}
print(names)

Output → {'Ghost', 'Raven', 'Wolf'}

Three unique names. The duplicates never made it in.

The fastest way to deduplicate a list:

attendees = ["Raven", "Wolf", "Raven", "Ghost", "Wolf", "Bull"]
unique = set(attendees)
print(unique)

Output → {'Bull', 'Ghost', 'Raven', 'Wolf'}

No order

Sets are unordered. The elements have no position — no index 0, no index 1.

Every time you print a set, the order may be different.

This means: no indexing, no slicing. You can't do names[0] on a set.

When to use a set

  • Removing duplicates from a list
  • Checking if a value exists — sets are faster than lists for this
  • Finding what two collections have in common — or don't

Use a list when order matters or duplicates are allowed.

Use a set when you only care about unique values.

What's really happening

A set doesn't store values in sequence. It stores them in a structure optimized for fast lookup.

Checking "Raven" in names on a set is nearly instant — even with a million elements.

On a list, Python has to scan from the start. On a set, it goes directly.

Heads up!

  • {} creates an empty dict — not an empty set. Use set() for an empty set
  • Sets are unordered — the output order is not guaranteed
  • Sets only store unique values — duplicates are silently dropped
  • Set elements must be immutable — strings, numbers, tuples are fine. Lists are not

The mindset shift

Stop thinking: "I'll use a list and filter duplicates later."

Start thinking: "If I only care about unique values — use a set from the start."

What you should understand now

  • A set stores only unique values — duplicates are dropped automatically
  • Created with curly braces or set()
  • Unordered — no indexing, no slicing
  • Use set(list) to deduplicate a list instantly
  • Fast membership checking — faster than lists
  • {} is an empty dict — use set() for an empty set
[ login to bookmark ] // copied! 18 views · 2 min
← prev Tuple Mini Project — Temperature Converter next → Using Sets in Your Code
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.