{(x, y) for x in range(3) for y in range(3) if x < y}
Built-in Data Structures

Meaning

A set comprehension that builds a set of 2‑tuples (x, y) by iterating over two ranges and keeping only the pairs where the first element is less than the second. It solves the need for a concise, memory‑efficient way to generate unique unordered pairs without writing explicit nested loops. You reach for it whenever you need all ordered pairs from a bounded integer domain that satisfy a simple relational condition.

Primary Function

Data transformation

Communicative Purpose

Enables creation of a set of coordinate pairs where the first element is strictly smaller than the second.

Pattern

{(a, b) for a in range(limit_a) for b in range(limit_b) if a < b}

Core Structure

{(..., ...) for ... in range(...) for ... in range(...) if ... < ...}

Função primária

Data transformation

Propósito comunicativo

Enables creation of a set of coordinate pairs where the first element is strictly smaller than the second.

Situações de gatilho

Mathematics: generating all ordered integer pairs (x, y) with x < y from a small range Algorithm design: building a set of candidate edges for an undirected graph from node indices Testing: producing all unique test case inputs that satisfy a relational constraint

Contextos

Data‑analysis scripts, algorithmic problem‑solving code, educational examples, competitive‑programming solutions

Padrão

{(a, b) for a in range(limit_a) for b in range(limit_b) if a < b}

Estrutura central

{(..., ...) for ... in range(...) for ... in range(...) if ... < ...}

Slots de substituição

a: int, b: int, limit_a: int ≥ 0, limit_b: int ≥ 0, condition: a < b

Colocados típicos

  • range()
  • if
  • for
  • tuple
  • set

Substituições comuns

  • list comprehension followed by set() conversion
  • nested for‑loops with manual add
  • itertools.product with a filter

Erros comuns

Using a mutable object (e.g., list) as the tuple element → TypeError because set elements must be hashable; reversing the comparison (a > b) → generates the opposite pair set; forgetting the parentheses around the tuple → creates a set of single values instead of pairs; using mismatched range limits that produce an empty set; omitting the if clause and unintentionally including equal pairs

Similar / contraste

list comprehension + set() cast – produces the same result but with an extra intermediate list; itertools.combinations – yields unordered pairs without needing an explicit condition

Interferências

Coming from JavaScript: you may try to use an array literal [] instead of a set literal {} → duplicates are not removed and the syntax is invalid in Python

Família do chunk

  • list_comprehension
  • generator_expression
  • itertools.combinations

Nuance

1) Do not use when the order of elements matters, because a set discards ordering. 2) Memory usage grows with the number of qualifying pairs; for large ranges consider itertools.combinations to avoid materialising the whole set. 3) Both elements of the tuple must be hashable, so custom mutable objects cannot be stored directly.

Efeito pragmático

Provides a one‑liner that generates all unique unordered pairs, reducing boiler‑plate code and minimizing the chance of off‑by‑one errors in nested loops.

Dica de memória

Think of the set comprehension as a dance floor where every dancer (x) only invites a partner (y) who is taller, and the floor automatically records each unique couple.

Nota

Set comprehensions require each generated element to be hashable; tuples of immutable types satisfy this requirement.

Upgrade path

After mastering this set comprehension, progress to using itertools.combinations for lazy, memory‑efficient generation of unordered pairs, or use numpy.triu_indices for numeric arrays.

Frequência: HighFormulaicidade: FixedTipo de construção: set_comprehensionPrioridade de aquisição: Automatic productionPrioridade de output: BothTag de espaçamento: Immediate

Log in to save chunks.