Skip to Content
Course content

47: Practice Exercise: List and Set Manipulation Drills

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

Think about organizing a music festival. You have two different lists. First, you have the Artist Lineup. The order of this list is everything—who opens the show, who plays the midday slot, and who headlines at midnight. If you swap the order, you've completely changed the flow of the event. That's your list.

Then, you have the Security Wristband List. The security guard at the gate doesn't care who was signed up first or who is the headliner; they just need to know: "Is this person's name on the list? Yes or No?" They don't need duplicates, and they need to find the answer instantly without scanning the entire page from top to bottom. That's your set.

When Order Matters vs. When Only Presence Matters

In Python, we use lists when the sequence is the primary goal. If you're tracking the history of a user's clicks on a website, you need a list because the order tells a story. But the moment you start asking questions like "Has this user ever visited the pricing page?" scanning a list of 10,000 clicks is a waste of CPU cycles. I always tell my juniors: if you're using a list just to check for membership, you're using the wrong tool.

# The "Lineup" (List) - Order is preserved
lineup = ["Opening Act", "Midday Jazz", "The Headliner"]
lineup.append("Late Night DJ") # Adds to the end

# The "Wristband List" (Set) - Only presence matters
wristbands = {"Alice", "Bob", "Charlie"}
wristbands.add("Diana") # No order, just existence

The Magic of Casting for De-duplication

One of the most common patterns I use in production code is "casting" a list into a set and then back into a list. Why? Because it's the fastest way to kill duplicates. Imagine your festival guest list has "Alice" written on it three times because she registered via three different forms. You can't just leave it like that, or your headcount will be wrong.

By wrapping your list in set(), Python instantly throws away every duplicate. If you need that order back (or just need it to be a list again for further manipulation), you wrap it back in list(). It's a clean, one-line operation that saves you from writing a clunky for loop with an if not in check.

messy_guests = ["Alice", "Bob", "Alice", "Charlie", "Bob"]
unique_guests = list(set(messy_guests)) 
# Result: ['Alice', 'Bob', 'Charlie'] (though order might change!)

Slicing and Dicing with Set Logic

The real power of sets comes when you have two of them. This is where we get into set theory, but don't let the math term scare you—it's actually very intuitive. If you have a set of "People who bought VIP tickets" and a set of "People who bought Early Bird tickets," you can find the overlap (the intersection) almost instantly using the & operator.

I find that using these operators is much more readable than nested loops. Whether you're looking for common interests between two users or finding which emails in a list haven't been sent yet (using difference), sets are your best friend.

vip_guests = {"Alice", "Bob", "Charlie"}
early_birds = {"Charlie", "Diana", "Edward"}

# Who got both? (Intersection)
both = vip_guests & early_birds # {"Charlie"}

# Who is a VIP but NOT an early bird? (Difference)
exclusive_vips = vip_guests - early_birds # {"Alice", "Bob"}



📋 Practical Task

Exercise: The Social Media Tag Sanitizer

You've been handed a raw list of tags from two different blog posts. The data is messy: there are duplicates, and some tags are repeated across both posts. Your goal is to clean this data for a database.

Your Task:

  1. Create two lists: post_one_tags = ["python", "coding", "tutorial", "python", "beginner"] and post_two_tags = ["python", "webdev", "tutorial", "advanced", "tutorial"].
  2. Create a new set called unique_all_tags that contains every tag used across both posts, with no duplicates.
  3. Create a set called shared_tags that contains only the tags that appear in both posts.
  4. Print the unique_all_tags as a sorted list.
  5. Print the shared_tags set.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.