Module 2 Lesson 5: Sets
·Programming

Module 2 Lesson 5: Sets

Master unique collections in Python. Learn how Sets work, how they handle duplicates, and how to use Set operations like Union and Intersection.

Module 2 Lesson 5: Sets

The last of Python's "Big Four" data structures is the Set. Sets are perfect for when you need a collection of items, but you don't care about their order and you want to ensure that every item is unique.

Lesson Overview

In this lesson, we will cover:

  • Creating Sets: Using curly braces {} or the set() function.
  • Uniqueness: How sets automatically handle duplicates.
  • Set Operations: Union, Intersection, and Difference.
  • Performance: Why sets are so fast at checking membership.

1. Creating a Set

Sets use curly braces {}, like dictionaries, but they only contain values (no keys).

unique_numbers = {1, 2, 3, 4, 5}
names = {"Alex", "Sara", "Bill"}

The Duplicate Trap: If you try to add a duplicate, Python simply ignores it!

numbers = {1, 2, 2, 3, 3, 3}
print(numbers) # Output: {1, 2, 3}

2. Converting to Sets

A very common pro trick is converting a List with duplicates into a Set to quickly clean up data.

messy_list = ["apple", "banana", "apple", "cherry", "banana"]
clean_set = set(messy_list)
print(clean_set) # Output: {'cherry', 'apple', 'banana'}

3. Set Operations (The Mathy Stuff)

Sets are powerful because they allow you to perform "Venn Diagram" operations.

set_a = {1, 2, 3}
set_b = {3, 4, 5}

# Union (All items from both)
print(set_a | set_b) # Output: {1, 2, 3, 4, 5}

# Intersection (Only items in BOTH)
print(set_a & set_b) # Output: {3}

# Difference (Items in A but NOT in B)
print(set_a - set_b) # Output: {1, 2}

4. Why Use Sets?

The main reason to use a set (besides uniqueness) is Membership Testing. Asking Python if "Alex" in a_set is incredibly fast, even if the set has millions of items. In a List, Python has to check every item one-by-one; in a Set, it knows exactly where to look.


Practice Exercise: The Unique ID Generator

Create a file named unique_ids.py.

  1. Create a list named user_input_ids that contains several duplicate numbers (e.g., [101, 102, 101, 103, 102, 104]).
  2. Convert that list into a set named valid_ids.
  3. Add a new ID 105 to the set using .add().
  4. Remove ID 102 from the set using .remove().
  5. Check if ID 101 is inside the set and print "Active ID found!" if it is.
  6. Print the final set and the count of unique IDs.

Quick Knowledge Check

  1. What symbols do we use to create a Set?
  2. What happens if you add a value to a Set that already exists in it?
  3. What does the & operator do between two sets?
  4. True or False: Sets are ordered.

Key Takeaways

  • Sets are unordered collections of unique items.
  • They are optimized for membership testing (in operator).
  • Use set() to quickly remove duplicates from other collections.
  • Union and Intersection are powerful tools for comparing data groups.

What’s Next?

We’ve now mastered all four major data structures. But when should you use which? In Lesson 6, we’ll wrap up the structures section by learning how to Choose the Right Data Structure for any job!

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn