DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 53 of 77
←PreviousPrevNextNext→

Set Exercises

Beginner Exercises

  • Create a set from a list with duplicates to show automatic deduplication
  • Check if two sets share any elements using intersection
  • Find the difference between two sets
  • Combine two sets into a union without duplicates
  • Check if one set is a subset of another
Example
# Remove duplicates from a list using a set
numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = set(numbers)
print(unique_numbers)
Output
{1, 2, 3, 4, 5}

Intermediate Challenges

ProblemInput ExampleExpected Output
Find common elements{1, 2, 3}, {2, 3, 4}{2, 3}
Find unique elements{1, 2, 3}, {2, 3, 4}{1, 4}
Check subset{1, 2}, {1, 2, 3, 4}True
Remove multiple items{1, 2, 3, 4, 5}, [2, 4]{1, 3, 5}
Perform set operations{1, 2, 3}, {3, 4, 5}Union: {1, 2, 3, 4, 5}, Intersection: {3}

Advanced Problems

Work with multiple sets to solve more complex problems, such as finding elements that occur in only one of the sets.

Example
# Find elements that appear in only one of three sets
def unique_elements(*sets):
    all_elements = set()
    seen_multiple = set()
    
    for s in sets:
        for element in s:
            if element in all_elements:
                seen_multiple.add(element)
            all_elements.add(element)
    
    return all_elements - seen_multiple

set1 = {1, 2, 3}
set2 = {2, 3, 4}
set3 = {3, 4, 5}

print(unique_elements(set1, set2, set3))  # {1, 5}
Output
{1, 5}
Test your knowledge: Set Exercises
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 53 of 77
←PreviousPrevNextNext→