Language Syntax

Dict comprehension — Python syntax

Build a dictionary eagerly from key and value expressions. Keys must be hashable; later equal keys replace earlier values. In 3.13 the key expression is evaluated before the value.

Formal Syntax & Structure

python syntaxpython
{key: value for target in iterable if condition}

Return Value

dict

Value obtained by evaluating this expression; see description for context and constraints.

Working Example

Example Snippetpython
pairs=[('x',1),('y',2),('x',3)]print({k:v for k,v in pairs})
Python Reference