[Python-Dev] dict comps
M.-A. Lemburg
mal@lemburg.com
Fri, 26 Oct 2001 11:02:02 +0200
Tim Peters wrote:
>
> [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'}
Cool !
> 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.
One way or another, you'll use up a giant chunk or two of data
on the heap... I'd suggest adding a new builtin
huge_file_as_mapping_apply('file', f) ;-)
Seriously, this goes down the path of lazy evaluation of expressions.
Not sure whether this is the right path to follow though (can cause
brain damage due to overloaded builtin debuggers, IMHO).
BTW, looks like I can finally get rid off the dict() builtin I
have in mxTools which is Good News !
--
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
______________________________________________________________________
Consulting & Company: http://www.egenix.com/
Python Software: http://www.lemburg.com/python/