Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Unique Collections and Fast Membership Checks

Sets help you think about uniqueness instead of order or labels

Lists focus on sequence. Dictionaries focus on lookup by key. Sets solve a different problem: you care most about whether a value is present and whether duplicates should collapse into one unique collection.

Core Explanation

Sets are useful for:

  • removing duplicates

  • testing whether a value exists

  • comparing overlapping groups

Continuity from dictionaries

Dictionaries answer “what value belongs to this key?” Sets answer “is this value present, and which values are unique or shared?”

customers = {"C001", "C002", "C002", "C003"}
print(customers)
print("C001" in customers)

Worked Example: remove duplicates and test membership

The code above shows the two main beginner uses of sets at once: duplicate values collapse automatically, and membership checks read clearly with in.

Exercises

  1. Create a set of product categories.

  2. Remove duplicates from a small list by converting it to a set.

  3. Explain why sets are good for membership checks.

  4. Give one example where uniqueness matters more than order.

Hint

If you only care whether a value appears and you do not want duplicates, a set is usually a strong fit.

Quick Summary
  • sets store unique values

  • sets support efficient membership testing

  • sets are useful for overlap and uniqueness problems

  • sets are best when order is not the main concern

Practice Lab

Expected output
{101, 102, 103, 104}
Expected output
True
2

Set Methods

Use these short examples to practice one set method at a time.

add()

Use add() to place one new value into a set.

update()

Use update() to merge several new values into the set.

discard()

Use discard() to remove a value without raising an error if it is missing.

remove()

Use remove() when the value must exist and you want to delete it.

union()

Use union() to combine values from two sets.

intersection()

Use intersection() to keep only shared values.

Guided Practice

What makes a set different from a list?

A set stores only integersSets can store many hashable value types.
A set stores unique valuesCorrect. Sets remove duplicates.
A set always keeps insertion order as the main guaranteeUniqueness is the main concept to emphasize here.
A set stores key-value pairsThat is a dictionary behavior.

Why is `len(regions)` equal to 2 in the example?

Because strings do not countStrings do count as set elements.
Because duplicate values are removedCorrect. `West` appears once in the final set.
Because sets always have length 2Set length depends on its elements.
Because one value is hiddenNothing is hidden; duplicates are simply collapsed.

Key Takeaway

Sets are the natural choice when uniqueness and membership matter more than position or labels. They make deduplication and overlap checks simple to express.

Deeper Python Set Patterns

Adapted for this course from the Python Tutorial data structures chapter.

This section extends the core set idea into patterns you will use often in real Python code:

  • creating sets from repeated values

  • set algebra operations such as union and intersection

  • membership tests

  • set comprehensions

How to read this section

Focus on the question each pattern answers: remove duplicates, combine groups, find overlap, or build a filtered unique collection.

# Set creation and basic operations
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print('Unique basket items:', basket)
print('orange in basket?', 'orange' in basket)

a = set('abracadabra')
b = set('alacazam')
print('a:', a)
print('b:', b)

print('Difference (a - b):', a - b)
print('Union (a | b):', a | b)
print('Intersection (a & b):', a & b)
print('Symmetric difference (a ^ b):', a ^ b)

# Set comprehension
comp = {x for x in 'abracadabra' if x not in 'abc'}
print('Comprehension result:', comp)