Sets in Python

Python tutorial · PySpark.in

Sets

A set in Python is a collection of unique, unordered,mutable and unique elements. Sets are mainly used when you want to remove duplicates or perform mathematical operations like union, intersection, etc.

What makes Sets Special

  • No duplicates allowed

    • Unordered (no indexing)

    • Mutable (you can add/remove items)

    • Fast for membership checks (like in)

    • Performs math operations easily


  • Creating a Set

    Defining a set using curly braces {} or the set() function.

    PYTHON
    1# Creating a simple set
    2my_set = {1, 2, 3, 4}
    3# Using set() to remove duplicates
    4numbers = set([1, 2, 2, 3, 4])
    5print(numbers) # Output: {1, 2, 3, 4}
    ▶ Output will appear here.

    Note:

    PLAINTEXT
    1s = {} # This creates a dictionary, not a set
    2s = set() # Correct way to create an empty set
    ▶ Output will appear here.

    Accessing Set Items

    Sets cannot be accessed using indexes; they can only be looped through.

    PYTHON
    1fruits = {"apple", "banana", "mango"}
    2for item in fruits:
    3 print(item)
    ▶ Output will appear here.

    Adding Elements to a Set

    add() – add a single element

    Adds a single new element to the set.

    PYTHON
    1s = {1, 2, 3}
    2s.add(4)
    3print(s)
    ▶ Output will appear here.

    update() – add multiple elements

    Adds multiple elements (from lists, sets, or iterables) to the set.

    PYTHON
    1s = {1, 2, 3}
    2s.update([5, 6, 7])
    3print(s)
    ▶ Output will appear here.

    Removing Elements

    Deletes a specific item from the set but raises an error if the item doesn't exist.

    PYTHON
    1s = {1, 2, 3, 4}
    2s.remove(2) # Removes 2 (error if not found)
    3s.discard(5) # Removes 5 (no error if not found)
    4s.pop() # Removes a random element
    5s.clear() # Removes all elements
    6print("Nothing is there in the Set")
    ▶ Output will appear here.

    Set Operations (Math Operations)

    1. Union (A ∪ B)

    Returns all unique elements from both sets combined.

    PYTHON
    1a = {1, 2, 3}
    2b = {3, 4, 5}
    3print(a.union(b)) # {1, 2, 3, 4, 5}
    ▶ Output will appear here.

    2. Intersection (A ∩ B)

    Returns elements that are common to both sets.

    PYTHON
    1a = {1, 2, 3}
    2b = {3, 4, 5}
    3print(a.intersection(b)) # {3}
    ▶ Output will appear here.

    3. Difference (A – B)

    Returns elements present in the first set but not in the second.

    PYTHON
    1a = {1, 2, 3}
    2b = {3, 4, 5}
    3print(a.difference(b)) # {1, 2}
    ▶ Output will appear here.

    4. Symmetric Difference

    Returns elements that are in either set but not in both.

    PYTHON
    1a = {1, 2, 3}
    2b = {3, 4, 5}
    3print(a.symmetric_difference(b)) # {1, 2, 4, 5}
    ▶ Output will appear here.

    Checking Membership

    Checks whether a value exists inside a set using in.

    PYTHON
    1colors = {"red", "blue", "green"}
    2print("red" in colors) # True
    3print("black" in colors) # False
    ▶ Output will appear here.

    Example: Remove Duplicates from a List

    Converts a list into a set to eliminate duplicate values.

    PYTHON
    1nums = [1, 2, 2, 3, 4, 4, 5]
    2unique_nums = list(set(nums))
    3print(unique_nums)
    ▶ Output will appear here.

    Useful Set Methods

    Set Methods in Python

    Method

    Description

    add()

    Add one item

    remove()

    Remove item (error if missing)

    discard()

    Remove item (no error)

    pop()

    Remove random item

    clear()

    Remove all items

    union()

    Combine sets

    intersection()

    Common elements

    difference()

    Items only in one set

    symmetric_difference()

    Items not common

    issubset()

    Checks if A is subset of B

    issuperset()

    Checks if A is superset of B

    isdisjoint()

    No common elements

    More Python tutorials

    All tutorials · Try the free PySpark compiler · Practice challenges