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:
- Set Type in Python: fundamentals, syntax, and operations
- Basic Python syntax
- Working with variables
- Input and Output operations
- Understanding collection types (especially differences between lists and sets)
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
-
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. - 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.
- Using the wrong type: Beginners sometimes use a list instead of a set, which doesn't automatically remove duplicates.
-
Missing punctuation handling: If a sentence contains commas or periods, they may be included in words. Consider using
.strip()
or.replace()
if needed. - 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.
- Ask the user to input a sentence
- Split the sentence into a list of words
- Convert the list to a set
- 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
- Using a list instead of a set: Lists don’t prevent duplicates. To get only unique values, always use a set.
-
Placing
set()
inside the loop: If you redefine the set in every iteration, it resets - you’ll end up with only the last entry. -
Capitalization mismatch: If one user enters "Blue" and another enters "blue", they’ll be treated as different. Consider normalizing with
.lower()
. -
Not using
add()
correctly: Remember, sets useadd()
, notappend()
. - 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.
- Create an empty set before the loop
- Use a
for
loop to prompt 3 users - Normalize each color (e.g. use
.lower()
) - Add each color to the set using
add()
- 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
- Not normalizing input: If you don’t apply
.strip()
or.lower()
, "Alice" and "alice" will be treated as different members. -
Using list methods instead of set methods: Make sure you use set operations like
intersection()
, notlist.index()
or similar. - Forgetting to convert input to set: If you try to use set operations on lists, the code will either break or behave incorrectly.
- Incorrect operation usage: Mixing up
intersection
anddifference
may lead to wrong results - know the difference clearly. - 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.
- Ask the user to enter names for the Python Club
- Split and normalize the names
- Repeat for the AI Club
- Convert both lists to sets
- Use
&
to find users in both sets - Use
-
to find Python-only users - Use
|
to find all unique users - 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
- Modifying the original allowed set: Always work on a copy of the user’s input - do not alter the reference set.
- Incorrect set logic: Be sure you understand the difference between intersection and difference - they serve opposite purposes.
- Not stripping whitespace:
"book"
and" book"
are different unless you trim input properly. - Using list comparisons instead of set comparisons: Sets make this task much easier and more accurate.
- 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.
- Define a set of allowed items
- Ask the user to input items they packed
- Convert their input into a normalized set
- Use
&
to find allowed items - Use
-
to find disallowed items - 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
orAccess 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()
orrequired_tags.issubset(user_tags)
Common Mistakes
- Case inconsistency: If the user enters
"Admin"
and the system expects"admin"
, the comparison fails. Always normalize. -
Incorrect set direction:
user_tags.issuperset(required_tags)
andrequired_tags.issubset(user_tags)
are equivalent - don’t reverse them. - Partial matches allowed incorrectly: If only some tags match, full access should not be granted - make sure you're checking the full subset.
- Hardcoding user input: Always collect user data via
input()
to keep the logic flexible. - 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?
- Ask the user to input tags (comma-separated)
- Clean and convert the tags into a set
- Define the required tags internally in your script
- Use
.issuperset()
or.issubset()
to compare - 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
-
Wrong intersection logic: When checking common tags, use
user1 & user2 & user3
- pairwise&
may lead to incomplete results if missed. - Not normalizing input: Inconsistent case or spacing will prevent proper comparisons - normalize every tag.
- 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.
- Forgetting to use sets: Using lists will lead to duplicates and harder comparisons.
- 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.
- Ask three users to enter their tags (comma-separated)
- Convert each input into a cleaned set
- Use
&
to find tags common to all users - Use
-
to find each user's exclusive tags - Use
|
to get the total number of distinct tags - 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