-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
47: Practice Exercise: List and Set Manipulation Drills
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:
- Create two lists:
post_one_tags = ["python", "coding", "tutorial", "python", "beginner"]andpost_two_tags = ["python", "webdev", "tutorial", "advanced", "tutorial"]. - Create a new set called
unique_all_tagsthat contains every tag used across both posts, with no duplicates. - Create a set called
shared_tagsthat contains only the tags that appear in both posts. - Print the
unique_all_tagsas a sorted list. - Print the
shared_tagsset.
There are no comments for now.