{len(word) for word in
Built-in Data Structures

Meaning

Computes the length of each element in an iterable and collects the unique lengths into a set. This avoids dealing with duplicate lengths when you only care about distinct sizes. Use it when you need to know which lengths appear in a collection of strings or other items.

Primary Function

Unique length extraction

Communicative Purpose

Enables working with distinct lengths of items for filtering or grouping by length.

Pattern

{len(item) for item in iterable}

Core Structure

{len(item) for item in iterable}

Função primária

Unique length extraction

Propósito comunicativo

Enables working with distinct lengths of items for filtering or grouping by length.

Situações de gatilho

Text analysis: needing to know which word lengths appear in a list of strings.

Contextos

Data processing, algorithm challenges, text analysis, any situation where unique lengths matter.

Padrão

{len(item) for item in iterable}

Estrutura central

{len(item) for item in iterable}

Slots de substituição

{item: variable representing each element, iterable: any iterable of strings}

Colocados típicos

  • len
  • for
  • in
  • set
  • list
  • string

Substituições comuns

  • replace len with other functions (e.g.
  • str.upper)
  • replace iterable with any collection
  • use different variable name

Erros comuns

forgetting braces yields a generator; using list comprehension yields duplicates; forgetting to apply len

Similar / contraste

list comprehension [len(w) for w in ...] keeps duplicates; map(len, ...) returns a map object

Interferências

Coming from Python: confusing set comprehension with list comprehension → remember that set comprehension uses braces and yields unique values.

Família do chunk

  • set comprehensions

Nuance

Do not use when you need to preserve duplicate lengths or the order of lengths; the operation is O(n) time and O(k) memory where k is the number of unique lengths; note that non-string iterables work as long as len() is defined, but objects without len() raise TypeError.

Efeito pragmático

signals that the speaker cares only about distinct lengths, not frequency or order

Dica de memória

Imagine throwing all your word lengths into a bag that automatically discards duplicates—only distinct sizes remain.

Nota

None

Upgrade path

consider using map(len, ...) wrapped in set() for functional style, or pandas.Series.str.len().unique() for dataframes

Frequência: HighFormulaicidade: Semi-fixedTipo de construção: set comprehensionPrioridade de aquisição: Automatic productionPrioridade de output: BothTag de espaçamento: Immediate

Log in to save chunks.