Dict / set comprehension

Applies to: python

Like a list comprehension but building a dict or set in one expression, concise ways to construct mappings and unique collections.

square = {x: x*x for x in range(4)}   # {0:0, 1:1, 2:4, 3:9}
evens  = {x for x in nums if x%2==0}  # set comprehension

See also: list-comprehension, hash-map, set