Meaning
Creates a set of immutable frozensets from an iterable of pairs, removing duplicates where pair order does not matter. Useful for deduplicating unordered pairs such as graph edges or coordinates where (a,b) and (b,a) should be considered the same.
Primary Function
Deduplication
Communicative Purpose
Convert a list of pairs into a collection of unique unordered pairs for efficient set operations.
Pattern
{frozenset(pair) for pair in iterable}
Core Structure
{frozenset(...) for ... in ...}
Função primária
Deduplication
Propósito comunicativo
Convert a list of pairs into a collection of unique unordered pairs for efficient set operations.
Situações de gatilho
Graph processing: deduplicate undirected edges; Computational geometry: treat coordinate pairs as unordered
Contextos
Graph processing, computational geometry, interview problems involving unordered pairs.
Padrão
{frozenset(pair) for pair in iterable}
Estrutura central
{frozenset(...) for ... in ...}
Slots de substituição
iterable: collection of pairs (e.g., list of tuples), pair: variable representing each pair (typically a tuple of two elements)
Colocados típicos
- Often followed by set operations like union
- intersection
- difference
- or membership tests
- used with graph libraries or algorithms requiring hashable edges.
Substituições comuns
- {tuple(sorted(pair)) for pair in iterable} (preserves order via sorting) or using dict.fromkeys to deduplicate while keeping original pair objects.
Erros comuns
Using a regular set instead of frozenset leads to "unhashable type: 'set'" error; assuming that frozenset preserves order (it does not).
Similar / contraste
Set of tuples {(a,b) for a,b in iterable} preserves order and treats (a,b) and (b,a) as distinct; using frozenset on each pair makes the pair order‑insensitive.
Interferências
Coming from languages where sets can store any objects (e.g., JavaScript arrays), one might forget that Python set elements must be hashable, thus needing frozenset for mutable pairs.
Família do chunk
- set comprehension
- frozenset
- deduplication patterns
- hashable wrappers
Nuance
The resulting set discards duplicate unordered pairs; if you need to retain duplicates or preserve original order, use a list or collections.Counter instead.
Efeito pragmático
Enables efficient deduplication of unordered pairs and allows fast set‑based operations such as membership testing and intersection.
Dica de memória
Think frozenset for hashable, order‑insensitive pairs.
Nota
Use this pattern when you need to treat (a,b) and (b,a) as identical, e.g., for undirected graph edges. Remember that frozenset discards duplicate elements and does not preserve order; if you need to keep the original order of each pair, use tuple(sorted(pair)) instead. Also ensure each pair's elements are hashable (no mutable types like lists).
Upgrade path
Use collections.Counter to count occurrences of each unordered pair, or adopt a graph library (e.g., networkx) that handles edge deduplication automatically.
Log in to save chunks.