fruits | {'orange'}
Built-in Data Structures

Meaning

The expression creates a new set that contains all elements of the left‑hand set together with the elements of the right‑hand literal set. It is useful when you need to extend a set without altering the original, avoiding side‑effects in functional‑style code. Use it whenever you want an immutable union of a set and a small collection of items.

Primary Function

Set manipulation

Communicative Purpose

Adds one or more elements to a set immutably.

Pattern

set_var | {element}

Core Structure

... | {...}

Função primária

Set manipulation

Propósito comunicativo

Adds one or more elements to a set immutably.

Situações de gatilho

Data processing: need a new set that includes additional items without changing the original collection; Algorithm design: combine a computed set with a constant literal set of values; Functional programming: avoid mutating input sets when extending them.

Contextos

Python code that works with sets, e.g., data processing, algorithm implementations.

Padrão

set_var | {element}

Estrutura central

... | {...}

Slots de substituição

set_var: identifier representing a set; elements: comma-separated literals or expressions to add

Colocados típicos

  • set.update
  • set.add
  • set.union
  • set.difference

Substituições comuns

  • fruits.union({'orange'})
  • fruits | set(['orange'])

Erros comuns

Using | with non-set operands causing TypeError; forgetting that the original set is unchanged; confusing with bitwise OR on integers.

Similar / contraste

fruits &= {'orange'} (in-place intersection); fruits -= {'orange'} (in-place difference); fruits = fruits | {'orange'} (same but explicit assignment).

Interferências

Coming from C, Java, JavaScript: may treat | as bitwise OR — in Python, | on sets performs union; confirm operands are sets.

Família do chunk

  • set union
  • set intersection
  • set difference

Nuance

Do not use when mutating the original set is required; performance: creates a new set, O(len(left)+len(right)) memory and time; boundary: left operand must be a set, right operand any iterable (converted to set), TypeError if left is not a set.

Efeito pragmático

Makes intent to combine sets explicit and immutable, reducing accidental side effects.

Dica de memória

Think of the pipe as a funnel pouring new fruit into a basket.

Nota

The left operand must be a set; the right operand is any iterable and is implicitly converted to a set. The original set is left unchanged; to modify the set in place use set.update() or the |= operator.

Upgrade path

Use set comprehension or .update for in-place mutation: fruits.update({'orange'}).

Frequência: MediumFormulaicidade: Semi-fixedTipo de construção: set union using the pipe (|) operator (immutable union)Prioridade de aquisição: Active recallPrioridade de output: BothTag de espaçamento: Medium-termIdioma?: Sim

Log in to save chunks.