flattening a dict
Duncan Booth
duncan.booth at invalid.invalid
Mon Feb 18 09:03:20 EST 2008
Boris Borcic <bborcic at gmail.com> wrote:
> It is more elementary in the mathematician's sense, and therefore
> preferable all other things being equal, imo. I've tried to split
> 'gen' but I can't say the result is so much better.
>
> def flattendict(d) :
> gen = lambda L : (x for M in exp(L) for x in rec(M))
> exp = lambda L : (L+list(kv) for kv in L.pop().iteritems())
> rec = lambda M : gen(M) if isinstance(M[-1],dict) else [M]
> return dict((tuple(L[:-1]),L[-1]) for L in gen([d]))
Why, why, why, why are you using lambda here? It only makes the code harder
to read (and it is bad enough without that). A lambda which is assigned
directly to a variable is a bad code smell.
More information about the Python-list
mailing list