Fundamentals 7 min read

Master Python Sets: Creation, Operations, and Quick Reference Guide

This guide explains Python sets—unordered, mutable collections of unique hashable items—covering their key characteristics, creation methods, set comprehensions, core API for adding, removing, and testing membership, as well as set algebra operations, practical examples, limitations, and a quick reference table.

Code Mala Tang
Code Mala Tang
Code Mala Tang
Master Python Sets: Creation, Operations, and Quick Reference Guide

What is a Python set?

In Python, a set is an unordered, mutable collection of unique hashable elements. It is ideal for membership testing, deduplication, and performing mathematical operations such as union, intersection, and difference.

Key characteristics

Unordered : Elements have no fixed position.

Mutable : Elements can be added or removed.

No duplicates : Duplicate entries are automatically eliminated.

Elements must be immutable (e.g., int, str, tuple).

Creating sets

# Using curly braces
my_set = {1, 2, 3}
# Using the set() constructor
my_set = set([1, 2, 3, 3, 4])  # {1, 2, 3, 4}

To create an empty set you must use set(); {} creates an empty dictionary.

Set comprehension

squares = {x*x for x in range(5)}  # {0, 1, 4, 9, 16}

Basic set methods

add(elem)

– add a single element. update(iterable) – add multiple elements. remove(elem) – remove element, raise KeyError if absent. discard(elem) – remove element if present, no error otherwise. pop() – remove and return an arbitrary element. clear() – empty the set. in / not in – membership test.

Set algebra

union()

or a | b – union of two sets. intersection() or a & b – intersection. difference() or a - b – elements in a but not in b. symmetric_difference() or a ^ b – elements in either set but not both. issubset() – true if all elements of a are in b. issuperset() – true if b is subset of a. isdisjoint() – true if sets share no elements.

Practical examples

# Remove duplicates from a list
unique = set([1, 2, 2, 3, 4, 4])  # {1, 2, 3, 4}
# Fast membership test
if 3 in unique:
    print("3 is in the set")
# Find common and unique elements
a = {1, 2, 3}
b = {2, 3, 4}
print(a & b)  # {2, 3}
print(a | b)  # {1, 2, 3, 4}
print(a - b)  # {1}
print(a ^ b)  # {1, 4}

Limitations

Sets cannot contain mutable (unhashable) types such as lists or other sets.

Because sets are unordered, they do not support indexing or slicing.

Quick reference

Creating a set: {1, 2, 3} or set([1, 2, 3]) Adding an element: s.add(x) Batch update: s.update([x, y]) Removing an element: s.remove(x) (error if missing) or s.discard(x) (no error).

Random pop: s.pop() Clear: s.clear() Union: s | t or s.union(t) Intersection: s & t or s.intersection(t) Difference: s - t or s.difference(t) Symmetric difference: s ^ t or s.symmetric_difference(t) Subset test: s <= t or s.issubset(t) Superset test: s >= t or s.issuperset(t) Disjoint test:

s.isdisjoint(t)
Data Structurestutorialcollection
Code Mala Tang
Written by

Code Mala Tang

Read source code together, write articles together, and enjoy spicy hot pot together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.