Creating Sets in Python: Harnessing the Power of Unique Collections introduces one of Python’s most efficient and mathematically elegant data structures — the set. Sets store unique, hashable elements with no duplicates and no inherent order (though insertion order is preserved in CPython since 3.7). They excel at membership testing (O(1) average time), deduplication, and set algebra (union, intersection, difference, symmetric difference) — making them ideal for uniqueness enforcement, overlap detection, filtering invalid data, and comparing collections. In 2026, sets remain indispensable in data science (unique values, categorical filtering), software engineering (permission sets, tag collections, cache keys), and algorithms (graph vertices/edges, reconciliation) — especially when combined with tuples for composite uniqueness, frozensets for hashability, and Polars/Dask/pandas for large-scale unique operations.
Here’s a complete, practical guide to creating sets in Python: literal syntax, set() constructor, deduplication patterns, real-world examples (earthquake country uniqueness, event deduplication, invalid data filtering), and modern best practices with type hints, performance, frozensets, and integration with Polars/pandas/Dask/NumPy.
1. Creating Sets — Literals, Constructors, from Iterables
# Set literal (preferred for static, small sets)
fruits = {"apple", "banana", "orange"}
print(fruits) # {'banana', 'orange', 'apple'} — order arbitrary
# Empty set (note: {} creates an empty dict!)
empty = set()
print(empty) # set()
# From list (automatic deduplication)
numbers = [1, 2, 3, 2, 4, 3, 5]
unique_nums = set(numbers)
print(unique_nums) # {1, 2, 3, 4, 5}
# From string (unique characters)
word = "hello"
unique_chars = set(word)
print(unique_chars) # {'h', 'e', 'l', 'o'}
# From range or other iterables
evens = set(range(0, 10, 2)) # {0, 2, 4, 6, 8}
print(evens)
2. Ensuring Uniqueness — Core Set Property
# Duplicates are automatically removed
duplicates = {1, 2, 2, 3, 3, 3, 4}
print(duplicates) # {1, 2, 3, 4}
# Mixed types (as long as hashable)
mixed = {1, "one", 2.0, (3, 4), True}
print(mixed) # order arbitrary, but unique
# Note: unhashable types (lists, dicts, sets) raise TypeError
# invalid = {[1, 2]} # TypeError: unhashable type: 'list'
3. Real-world pattern: earthquake data uniqueness & deduplication
import polars as pl
df = pl.read_csv('earthquakes.csv')
# Unique countries (fast deduplication)
unique_countries = set(df['country'].to_list())
print(f"Unique countries: {len(unique_countries)}")
print(sorted(unique_countries)[:5])
# Unique event identifiers (time + lat + lon)
event_ids = set(
(row['time'], row['latitude'], row['longitude'])
for row in df.iter_rows(named=True)
)
print(f"Unique events: {len(event_ids)} (deduplicated records)")
# Polars native unique (vectorized, often faster for large data)
unique_df = df.unique(subset=['time', 'latitude', 'longitude'])
print(f"Unique rows (Polars): {unique_df.shape[0]}")
# Countries with strong quakes (mag >= 7.0)
strong_countries = set(
row['country']
for row in df.filter(pl.col('mag') >= 7.0).iter_rows(named=True)
)
print("Countries with M7+ events:", sorted(strong_countries))
Best practices for creating sets in Python 2026
- Prefer set literals —
{"a", "b"}— for static, small sets (clean & readable). - Use set(iterable) — for deduplication:
set(lst),set(string),set(range(n)). - Use frozenset — when set must be hashable (dict key, set element):
frozenset([1,2,3]). - Avoid {} for empty sets — use
set()({} is empty dict). - Add type hints —
Set[int]orAbstractSet[str]. - Use Polars unique() — for large-scale unique values:
df['col'].unique(). - Use pandas unique() —
df['col'].unique(). - Use Dask unique().compute() — distributed unique.
- Use sets for validation —
required.issubset(available). - Use sets for filtering —
valid - invalid. - Use sets in config — unique allowed values.
- Use sets in caching — track seen items.
- Use sets in graph algorithms — adjacency sets.
- Use sets in rate limiting — unique IPs per minute.
- Use sets in anomaly detection — rare events.
- Use sets in data cleaning — remove invalid categories.
- Use sets in testing — assert unique count.
- Use set.add() — single element addition.
- Use set.update() — bulk addition from iterable.
- Use set.discard() — safe removal (no error if missing).
- Use set.remove() — error if missing.
- Use set.pop() — arbitrary removal & return.
- Use set.clear() — empty set.
- Use len(set) — cardinality.
- Use x in set — O(1) average membership.
- Use set.union() / | — new union.
- Use set.intersection() / & — new intersection.
- Use set.difference() / - — new difference.
- Use set.symmetric_difference() / ^ — new symmetric difference.
- Use issubset() / <= — subset check.
- Use issuperset() / >= — superset check.
- Use isdisjoint() — no common elements.
Sets provide fast, unique, unordered collections — ideal for deduplication, membership testing, and set algebra. In 2026, create sets with literals or set(), use frozenset for hashability, combine with tuples for composite keys, and leverage Polars/pandas/Dask for large-scale uniqueness. Master sets, and you’ll handle uniqueness, comparison, filtering, and overlap efficiently in any Python workflow.
Next time you need to enforce uniqueness or compare collections — reach for set. It’s Python’s cleanest way to say: “Unique elements only — no duplicates, no order, just fast membership and operations.”