๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
๐Ÿ Python

Sets (unique items)

๐Ÿ“š What are Sets? A set is an unordered collection that stores only unique items - duplicates are automatically removed. If you add the same item twice, the set only keeps one copy. Sets are extremely fast for checking membership (if x in my_set) which is far quicker than searching a list.

8 min 10 XP Lesson 18 of 21
Sets (unique items)
๐ŸŒ

Appy Saysโ€ฆ

Need to find every unique visitor to your app? Remove duplicate entries from a list? Check if two groups share any members? Python's set does all three instantly โ€” and it's way faster than a loop.

๐Ÿ“–

What is a Set?

A set is an unordered collection of unique items. Duplicate values are automatically ignored. Sets support fast membership testing (in) and powerful mathematical operations like union, intersection, and difference.

  • โ€ขCreate: my_set = {1, 2, 3} or set([1, 2, 2, 3]) โ†’ {1, 2, 3}
  • โ€ขEmpty set: set() โ€” NOT {} (that creates an empty dict!)
  • โ€ข.add(item) โ€” add one item; .remove(item) โ€” remove (error if missing); .discard(item) โ€” remove silently
  • โ€ขa | b โ€” union (all items from both)
  • โ€ขa & b โ€” intersection (items in both)
  • โ€ขa - b โ€” difference (items in a but not b)
  • โ€ขa ^ b โ€” symmetric difference (items in one but not both)
๐ŸŽฎ

Think of it like a Minecraft inventory with unique items only

Imagine an inventory where each item type can only appear once โ€” no stacks. If you try to add a second diamond sword, nothing changes. That's a set. And you can instantly compare two inventories to find what's common or what's exclusive to each.

โš™๏ธ

How It Works

  • โ€ข1. Sets use a hash table internally โ€” membership check is O(1), regardless of set size
  • โ€ข2. When you add a duplicate, Python silently ignores it โ€” no error
  • โ€ข3. Order is not guaranteed โ€” don't rely on iteration order
  • โ€ข4. Only hashable (immutable) items can be in a set: ints, strings, tuples โ€” not lists or dicts
  • โ€ข5. Convert a list to a set to remove duplicates: unique = list(set(my_list))
  • โ€ข6. item in my_set is much faster than item in my_list for large collections
๐ŸŒ

Real-World Examples

  • โ€ขCount unique daily users: len(set(user_ids_today))
  • โ€ขFind common friends: friends_of_alice & friends_of_bob
  • โ€ขFind songs in one playlist but not another: playlist_a - playlist_b
  • โ€ขRemove duplicate tags from a blog post: tags = list(set(tags))
  • โ€ขCheck if a username has been taken: if username in registered_users_set:
๐Ÿ’ก

Key Facts

  • โ€ขSet membership (in) is O(1) average โ€” checking a million-item set is as fast as checking a 10-item set
  • โ€ขLists have O(n) membership โ€” a set is ~100,000ร— faster for large collections
  • โ€ขPython also has frozenset โ€” an immutable set that can be used as a dictionary key
  • โ€ขSet comprehensions work just like list comprehensions: {x*2 for x in range(5)}
โš ๏ธ

Watch Out!

{} creates an empty dictionary, NOT an empty set. For an empty set you must write set(). This trips up almost every Python beginner at least once.

๐Ÿ“Œ

Remember

Use a set whenever you need uniqueness or fast membership checking. It's the right tool for 'have I seen this before?' logic.

โœ…

What You Learned

  • โ€ขSets store unique items; duplicates are silently ignored
  • โ€ขOperations: | union, & intersection, - difference โ€” all very fast
  • โ€ขUnlocks: deduplication, fast lookups, comparing groups, unique visitor counting

Key Facts

  • โ†’Set membership (in) is O(1) average โ€” checking a million-item set is as fast as checking a 10-item set
  • โ†’Lists have O(n) membership โ€” a set is ~100,000ร— faster for large collections
  • โ†’Python also has frozenset โ€” an immutable set that can be used as a dictionary key
  • โ†’Set comprehensions work just like list comprehensions: {x*2 for x in range(5)}

Real-World Examples

โ€ข Count unique daily users: <code>len(set(user_ids_today))</code> โ€ข Find common friends: <code>friends_of_alice & friends_of_bob</code> โ€ข Find songs in one playlist but not another: <code>playlist_a - playlist_b</code> โ€ข Remove duplicate tags from a blog post: <code>tags = list(set(tags))</code> โ€ข Check if a username has been taken: <code>if username in registered_users_set:</code>

Remember

Use a set whenever you need uniqueness or fast membership checking. It's the right tool for 'have I seen this before?' logic.

Quick Quiz

1 / 2

Sets contain?