Python Sets(advanced)

Introduction Reading Time: 10 min

Table of Contents

Description

A set is an unordered collection of unique items. Beyond basics, advanced set concepts include:
Set operations: union, intersection, difference, symmetric difference
Set comprehension
Frozen sets
Subset/superset checks
Removing duplicates from iterables
Performance use-cases (e.g., fast membership testing)

Prerequisites

  • Basic understanding of:
    What sets are
    Iterable data types (lists, tuples)
    Boolean logic and operations

Examples

Here's a simple program in Python:

✅ Set Operations
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

# Union (combine all unique elements)
print(a | b)  # {1, 2, 3, 4, 5, 6}

# Intersection (common elements)
print(a & b)  # {3, 4}

# Difference (elements in a not in b)
print(a - b)  # {1, 2}

# Symmetric Difference (elements in a or b, but not both)
print(a ^ b)  # {1, 2, 5, 6}
✅ Set Comprehension
# Create a set of squares from 0 to 4
squares = {x ** 2 for x in range(5)}
print(squares)  # {0, 1, 4, 9, 16}
✅ Removing Duplicates from a List
nums = [1, 2, 2, 3, 4, 4, 5]

# Convert to set to remove duplicates
unique = list(set(nums))
print(unique)  # [1, 2, 3, 4, 5]
✅ Frozen Sets
# Immutable version of set
frozen = frozenset([1, 2, 3])

# frozen.add(4)  # ❌ Error: 'frozenset' object has no attribute 'add'
print(frozen)  # frozenset({1, 2, 3})
✅ Subset and Superset Checks
a = {1, 2}
b = {1, 2, 3}

print(a.issubset(b))   # True
print(b.issuperset(a)) # True
✅ Disjoint Sets
# True if sets have no common elements
print({1, 2}.isdisjoint({3, 4}))  # True
print({1, 2}.isdisjoint({2, 3}))  # False

      

Real-World Applications

Fast duplicate removal in large datasets

Keyword filtering in NLP tasks

Unique identifiers (like tags, IDs)

Mathematical set operations in simulations

Permissions and roles management in applications

Where topic Can Be Applied

Data preprocessing in machine learning

Search optimization (e.g., auto-suggestions)

Web scraping: Keep track of already visited URLs

Graph theory: Set operations for neighbors and paths

Access control: Set-based role checking

Resources

Topic video source

A comprehensive video

Watch

Python pdf

pdf on topic

Visit

Interview Questions

What makes sets faster than lists for membership testing?

How does set() remove duplicates from a list?

Explain difference between |, &, -, and ^ in sets.

What is a frozenset and when would you use it?

How do sets handle duplicates internally?

What’s the difference between issubset() and issuperset()?