Skip to Content
Course content

38: Sets: Creating and Basic Operations

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine you're building a small analytics tool to track unique visitors to a website. You have a log file containing thousands of IP addresses, and you need to figure out exactly how many unique people visited your site. At first glance, this seems like a simple "keep a list" problem.

The O(n) Trap: Checking for Existence in Lists

If you're thinking like a beginner, you might reach for a list. You'd create an empty list, loop through every IP address in your log, and check if the IP is already in your list before adding it. It looks something like this:

unique_ips = []
for ip in log_data:
    if ip not in unique_ips:
        unique_ips.append(ip)

This works perfectly fine when you have ten or twenty visitors. But here is where we hit a wall as engineers: performance. Every time you call if ip not in unique_ips, Python has to scan the entire list from the beginning to the end to make sure that IP isn't hiding somewhere in the middle. In computer science terms, this is O(n) time complexity. If you have 100,000 logs, you're potentially performing billions of comparisons. Your script will hang, and your CPU will spike, all because you're using a tool designed for ordered sequences to solve a problem of uniqueness.

The Set Shortcut: Hashing for Constant Time

This is exactly why sets exist. A set in Python is essentially a dictionary that only stores keys without any values. Instead of scanning a list, a set uses a hash table. When you check if an item is in a set, Python doesn't "look" through the collection; it computes a hash of the item to jump directly to the memory location where that item would be. This is O(1), or constant time.

The most efficient way to handle our visitor problem is to either cast the entire list to a set or add items as you go:

# The fastest way if you already have the list
unique_ips = set(log_data)

# Or, if you're processing a stream of data
unique_ips = set()
for ip in log_data:
    unique_ips.add(ip)

I've seen countless junior devs stick with lists because they want to maintain the order of the elements. If order matters, we can talk about that in a later lesson, but for purely checking uniqueness or membership, the set is your best friend. It's cleaner, it's more intentional, and it's orders of magnitude faster.

Managing Your Collection Without the Overhead

Once you have a set, the operations are intuitive, though they differ slightly from lists. You don't append() to a set—because there is no "end" of a set—you add(). If you try to add an IP that's already there, Python just ignores it. No error, no duplicate, no problem.

You can also remove items using remove() or discard(). Here is a pro tip: use discard() if you aren't 100% sure the item exists. If you call remove() on an item that isn't in the set, Python will raise a KeyError and crash your program. discard(), on the other hand, will fail silently. I generally prefer discard() unless the absence of that item represents a genuine logic error in the application that should crash the program.

visitors = {"192.168.1.1", "10.0.0.1", "172.16.0.1"}

visitors.add("192.168.1.1") # Nothing happens, already exists
visitors.discard("10.0.0.1") # Removed successfully
visitors.discard("8.8.8.8")   # Not there, but no error is raised



📋 Practical Task

Cleaning the Newsletter Subscriber List

You have been handed a messy list of email addresses from a legacy marketing system. The list contains many duplicates because users signed up multiple times with different forms. Additionally, there are a few "blacklist" emails (like spam@bot.com and test@test.com) that must be removed from the final list before the campaign is sent.

Your Task:

  1. Create a list called raw_emails containing at least 10 emails, including several duplicates and at least two blacklist emails.
  2. Convert this list into a set to instantly remove all duplicates.
  3. Use the discard() method to remove the blacklist emails from your set.
  4. Print the final number of unique, clean subscribers using len().
Rating
0 0

There are no comments for now.

to be the first to leave a comment.