[Python-Dev] dict comps

Tim Peters tim.one@home.com
Fri, 26 Oct 2001 01:34:16 -0400


[Jeremy Hylton]
> We agreed yesterday that the dictionary() constructor would accept a
> a list of two-tuples (strictly speaking an iterable object of iterable
> objects of length 2).

FYI, this is checked in now.

> That plus list comprehensions pretty much covers the territory of
> dict comprehensions:
>
> >>> print dictionary([(i, chr(65 + i)) for i in range(4)])
> {0: 'A', 1: 'B', 2: 'C', 3: 'D'}

Wow -- that's *exactly* what it prints.  You got your own time machine now?

While it covers the semantics, the pragmatics may be off, since listcomps
produce genuine lists, and so e.g.

    dictionary([(key, f(key)) for key in file('huge')])

may require constructing an unboundedly large list of twoples before
dictionary() sees the first pair.  dictionary() per se doesn't require
materializing a giant list in one gulp.