Table of Contents

Python sets are powerful tools when you need to manage collections of unique items. Unlike lists, sets do not allow duplicates and have no guaranteed order. This makes them ideal for scenarios like filtering repeated values, comparing data sets, or performing fast membership checks. Sets also support useful operations like union, intersection, and difference - all of which can simplify logic for tasks involving distinct groups, permissions, or tags.

In this practice section, you’ll apply your understanding of set behavior to real-world tasks. These include removing duplicates from user input, comparing shared items across lists, and building logic based on set operations.

Before working through the exercises below, make sure you're familiar with the following topics:

Beginner-Level Practice – Set Type in Python

At the beginner level, working with sets focuses on core principles: how to create a set, add and remove elements, and eliminate duplicates. These tasks will help you become comfortable using basic set methods such as add(), remove(), and in. You’ll also see how sets handle uniqueness automatically and why they are useful for real-world scenarios like filtering input or comparing small groups of values. These exercises will reinforce your understanding of how sets differ from lists and why they’re more suitable in certain situations.

Exercise 1: Remove Duplicate Words

Ask the user to enter a sentence. Your task is to print only the unique words from that sentence, regardless of their repetition. For example, if the input is "apple banana apple orange banana", the output should be {"apple", "banana", "orange"}. Use a set to store and display the unique words.

What this Task Teaches

This task introduces you to the concept of automatic deduplication using sets. You’ll learn how to split a string into a list of words and convert that list into a set. It highlights how sets eliminate repeated values without additional logic. This pattern is useful when cleaning data, analyzing user input, or preparing content for comparison or search.

Hints and Tips
  • Use input() to get the sentence
  • Use .split() to turn the sentence into a list of words
  • Convert the list to a set to remove duplicates
  • Use print() to show the resulting set
Common Mistakes
  1. Forgetting to split the string: If you pass the full sentence directly into set(), it will treat each character as an element, not each word. Always split first.
  2. Assuming order matters: Sets do not preserve order. If your output order differs, it’s still correct as long as all unique words are present.
  3. Using the wrong type: Beginners sometimes use a list instead of a set, which doesn't automatically remove duplicates.
  4. Missing punctuation handling: If a sentence contains commas or periods, they may be included in words. Consider using .strip() or .replace() if needed.
  5. Over-formatting the output: Don’t attempt to reformat the set - just print it directly to avoid unnecessary complexity.
Step-by-Step Thinking

Think about how to extract data from the input, transform it, and filter it automatically.

  1. Ask the user to input a sentence
  2. Split the sentence into a list of words
  3. Convert the list to a set
  4. Print the set of unique words
How to Make it Harder
  • Make the comparison case-insensitive (e.g. "Apple" and "apple" should be the same)
  • Sort the final output alphabetically using sorted()
  • Count how many words were removed due to duplication

Exercise 2: Favorite Colors Survey

Simulate a simple survey where three users are asked to input their favorite color. After collecting all three inputs, your task is to display the list of unique favorite colors mentioned. Use a set to ensure no color is repeated in the result.

What this Task Teaches

This task helps you understand how sets can be used to gather and store non-repeating data. It demonstrates how to collect input repeatedly, update a set dynamically, and work with real-world data where repetition is common. It also reinforces the idea that sets grow only when a truly new value is added, which is helpful in statistics, surveys, and analytics.

Hints and Tips
  • Use a for loop to repeat the question three times
  • Initialize an empty set before the loop starts
  • Use add() to insert a color into the set
  • Print the final set after all inputs are collected
Common Mistakes
  1. Using a list instead of a set: Lists don’t prevent duplicates. To get only unique values, always use a set.
  2. Placing set() inside the loop: If you redefine the set in every iteration, it resets - you’ll end up with only the last entry.
  3. Capitalization mismatch: If one user enters "Blue" and another enters "blue", they’ll be treated as different. Consider normalizing with .lower().
  4. Not using add() correctly: Remember, sets use add(), not append().
  5. Forgetting to display the result: Ensure the final print statement is outside the loop to avoid redundant or partial output.
Step-by-Step Thinking

Imagine you are running a poll - your goal is to gather unique responses. Each new entry should be evaluated and added if it’s not already recorded.

  1. Create an empty set before the loop
  2. Use a for loop to prompt 3 users
  3. Normalize each color (e.g. use .lower())
  4. Add each color to the set using add()
  5. After the loop, display the set of favorite colors
How to Make it Harder
  • Ask for the user's name and associate it with their color using a dictionary
  • Allow any number of users by asking first how many will participate
  • Sort the output alphabetically and count how many unique colors were submitted

Intermediate-Level Practice – Set Type in Python

At the intermediate level, set operations become even more useful when working with multiple datasets. You’ll start using union, intersection, and difference to compare sets of data and make decisions based on relationships between groups. These patterns frequently appear in scenarios like filtering items, detecting duplicates, and comparing user permissions or survey results. Understanding how to manipulate and compare sets will give you a stronger foundation for solving more complex problems in data processing and analysis.

Exercise 1: Compare Club Memberships

You are given two groups of users: one who joined the Python Club and one who joined the AI Club. Your task is to ask the user to input the names of members in each group (comma-separated), then display:

  • Users who belong to both clubs
  • Users who belong only to the Python Club
  • All unique users from both groups

Use set operations to compare the two groups and display the results.

What this Task Teaches

This task strengthens your ability to apply set operations like intersection(), difference(), and union() in practical comparison scenarios. You’ll learn how to collect user input, normalize data, convert it into sets, and derive meaningful comparisons. These skills are essential when managing group data, filtering access, analyzing audiences, or working with categories in real-world applications.

Hints and Tips
  • Use split(",") to convert input into lists
  • Trim whitespace using .strip() and normalize names to lowercase
  • Convert both lists into sets before using operations
  • Use &, -, and | for set operations
Common Mistakes
  1. Not normalizing input: If you don’t apply .strip() or .lower(), "Alice" and "alice" will be treated as different members.
  2. Using list methods instead of set methods: Make sure you use set operations like intersection(), not list.index() or similar.
  3. Forgetting to convert input to set: If you try to use set operations on lists, the code will either break or behave incorrectly.
  4. Incorrect operation usage: Mixing up intersection and difference may lead to wrong results - know the difference clearly.
  5. Partial output: Ensure you print all three required result sets, not just one.
Step-by-Step Thinking

Think about the structure: you need to collect two groups, convert them into sets, and analyze the relationship between them.

  1. Ask the user to enter names for the Python Club
  2. Split and normalize the names
  3. Repeat for the AI Club
  4. Convert both lists to sets
  5. Use & to find users in both sets
  6. Use - to find Python-only users
  7. Use | to find all unique users
  8. Display all results clearly
How to Make it Harder
  • Ask for club names dynamically and support more than two groups
  • Track and display the number of users in each comparison group
  • Store club names and members in a dictionary and allow lookup

Exercise 2: Filter Allowed Items

Suppose you’re creating a system for checking allowed items in a backpack before boarding a plane. You have a predefined set of allowed items: {"laptop", "book", "charger", "phone", "headphones"}. The user enters a comma-separated list of items they packed. Your program should print:

  • All items that are allowed
  • All items that are NOT allowed

Use sets to filter and compare the lists.

What this Task Teaches

This task demonstrates how sets can be used as efficient filters to validate user-provided data. You’ll apply set intersections and differences to compare allowed vs actual items. This is a foundational use case for whitelist/blacklist scenarios - including security, validation, and permission-based systems. The focus is on comparing inputs with a reference set using fast and readable code.

Hints and Tips
  • Store the list of allowed items in a set
  • Split user input into a list and convert it to a set
  • Use & to find allowed items
  • Use - to find restricted items
Common Mistakes
  1. Modifying the original allowed set: Always work on a copy of the user’s input - do not alter the reference set.
  2. Incorrect set logic: Be sure you understand the difference between intersection and difference - they serve opposite purposes.
  3. Not stripping whitespace: "book" and " book" are different unless you trim input properly.
  4. Using list comparisons instead of set comparisons: Sets make this task much easier and more accurate.
  5. Overwriting sets during operations: Don’t overwrite your sets during operations - keep original references where needed.
Step-by-Step Thinking

Your goal is to compare two collections: what’s allowed vs what was actually packed. Think of this as a basic security checklist.

  1. Define a set of allowed items
  2. Ask the user to input items they packed
  3. Convert their input into a normalized set
  4. Use & to find allowed items
  5. Use - to find disallowed items
  6. Print both sets
How to Make it Harder
  • Suggest replacements for disallowed items
  • Add quantity restrictions (e.g., no more than 2 electronics)
  • Include nested categories like "electronics" or "personal care" using dictionaries

Advanced-Level Practice – Set Type in Python

At the advanced level, you’ll work with more complex set-based logic involving multiple datasets, dynamic inputs, and programmatic analysis. You’ll need to combine multiple operations and carefully consider edge cases such as empty sets, unknown values, or normalized formatting. These tasks mimic real-world logic used in systems for access control, data tagging, and analytics. Success here requires a solid grasp of how to construct and manipulate sets programmatically, and how to interpret what their relationships mean in context.

Exercise 1: Tag-Based Access Control

You are developing a tag-based access control system for a digital platform. Each user has a set of tags (like {"admin", "editor", "finance"}), and each page on the platform requires certain tags to be accessed (like {"admin", "editor"} for the Dashboard). Write a program that:

  • Asks the user to enter their tags (comma-separated)
  • Defines the required tags internally for a given page
  • Checks if the user has the required permissions (i.e., all required tags are present in the user's tag set)
  • Prints Access Granted or Access Denied accordingly
What this Task Teaches

This task models real-world logic for permission validation using sets. You'll practice subset comparisons (issubset()) and dynamic input handling to simulate role-based or tag-based access. It demonstrates how Python sets can be used to efficiently enforce permission logic without complex nested conditions or loops - a common requirement in user management systems and internal tool design.

Hints and Tips
  • Normalize input using .lower() and .strip() on each tag
  • Use split() to convert the input into a list, then to a set
  • Use .issuperset() or required_tags.issubset(user_tags)
Common Mistakes
  1. Case inconsistency: If the user enters "Admin" and the system expects "admin", the comparison fails. Always normalize.
  2. Incorrect set direction: user_tags.issuperset(required_tags) and required_tags.issubset(user_tags) are equivalent - don’t reverse them.
  3. Partial matches allowed incorrectly: If only some tags match, full access should not be granted - make sure you're checking the full subset.
  4. Hardcoding user input: Always collect user data via input() to keep the logic flexible.
  5. Not trimming whitespace: Extra spaces can cause comparisons to silently fail - always sanitize input.
Step-by-Step Thinking

This task is about evaluating permission rules: does a user meet all the required conditions?

  1. Ask the user to input tags (comma-separated)
  2. Clean and convert the tags into a set
  3. Define the required tags internally in your script
  4. Use .issuperset() or .issubset() to compare
  5. Print the appropriate message based on the result
How to Make it Harder
  • Support multiple pages with different permission sets
  • Allow a user to input a page name and match its required tags dynamically
  • Log all granted/denied attempts with timestamps

Exercise 2: Analyze Survey Tags

A survey collected user interests using tags such as "python", "ai", "ml", "backend", etc. You receive tag sets from 3 different users. Your task is to:

  • Display tags that are common to all users
  • Display tags that are unique to each user (i.e., not shared)
  • Print the total number of unique tags collected from all users

Use sets and their operations to complete the task.

What this Task Teaches

This task emphasizes advanced multi-set logic, especially working with intersections, symmetric differences, and unions. You’ll learn how to handle multiple inputs, calculate overlapping and exclusive sets, and produce insights across several collections. This approach is widely used in analytics, search systems, and data profiling. It also requires precise control of the logic for comparing more than two sets.

Hints and Tips
  • Normalize tags by converting them to lowercase and trimming whitespace
  • Use & operator multiple times to intersect all three sets
  • Use ^ (symmetric difference) to find tags unique to each user
  • Use | (union) to get the total number of distinct tags
Common Mistakes
  1. Wrong intersection logic: When checking common tags, use user1 & user2 & user3 - pairwise & may lead to incomplete results if missed.
  2. Not normalizing input: Inconsistent case or spacing will prevent proper comparisons - normalize every tag.
  3. Incorrect use of symmetric difference: Symmetric difference finds unique elements across two sets. To find per-user unique tags, compare with the union of others.
  4. Forgetting to use sets: Using lists will lead to duplicates and harder comparisons.
  5. Incorrect count of total tags: Only set.union() gives a proper distinct tag count - don’t sum the lengths of individual sets.
Step-by-Step Thinking

The challenge here is to derive insights from comparisons between multiple sets.

  1. Ask three users to enter their tags (comma-separated)
  2. Convert each input into a cleaned set
  3. Use & to find tags common to all users
  4. Use - to find each user's exclusive tags
  5. Use | to get the total number of distinct tags
  6. Print each of the three results clearly
How to Make it Harder
  • Support any number of users by using a loop and a list of sets
  • Count how many users selected each tag and rank them
  • Save the data in a dictionary and allow tag-based filtering

Author of This Exercises

Marcus Johnson

Marcus Johnson

I've been working as a Senior Python Backend Developer at the company StreamFlow Analytics...

Learn more →