Meaning
Assigns a new value to an existing or new key in a dictionary. Creates the key if it does not exist, otherwise overwrites the current value.
Primary Function
Data manipulation
Communicative Purpose
Update or insert a key-value pair in a dictionary.
Pattern
dictionary[key] = value
Core Structure
...[...] = ...
Função primária
Data manipulation
Propósito comunicativo
Update or insert a key-value pair in a dictionary.
Situações de gatilho
When you need to store a computed result under a specific key; when building a lookup table; when accumulating counts.
Contextos
Used in scripts, data processing pipelines, configuration builders, and any code that works with mapping objects.
Padrão
dictionary[key] = value
Estrutura central
...[...] = ...
Slots de substituição
dictionary: dict, key: hashable, value: any object
Colocados típicos
- dict.get
- dict.update
- defaultdict
- setdefault
Substituições comuns
- Using dict.setdefault(key
- value) if you only want to set when missing
- using dict.update({key: value})
Erros comuns
Using immutable key types like lists; forgetting that assignment overwrites existing value; confusing with equality check (==).
Similar / contraste
dict[key] += value (for numeric accumulation) vs direct assignment; dict.pop(key) removes.
Interferências
Coming from languages where arrays use bracket assignment but require pre-sizing (e.g., C, Java): learners may expect dictionary to need pre-allocation → dictionaries do not require pre-allocation.
Família do chunk
- Dictionary key access
- dictionary update
- dictionary comprehension
Nuance
Keys must be hashable; assignment does not raise KeyError for missing keys; performance is O(1) average.
Efeito pragmático
Provides a concise way to build or modify mappings, making intent explicit.
Dica de memória
Think of putting a label on a folder: dictionary[key] = value.
Nota
Note that dictionary assignment modifies the dictionary in place and returns None; attempting to use a mutable (unhashable) key raises TypeError.
Upgrade path
Using collections.defaultdict or collections.Counter for automatic default values; using dict comprehension for bulk creation.
Log in to save chunks.