[Python-ideas] Fwd: unpacking generalisations for list comprehension

Martti Kühne mar77i at mar77i.ch
Tue Oct 11 08:42:54 EDT 2016


Hello list

I love the "new" unpacking generalisations as of pep448. And I found
myself using them rather regularly, both with lists and dict.
Today I somehow expected that [*foo for foo in bar] was equivalent to
itertools.chain(*[foo for foo in bar]), which it turned out to be a
SyntaxError.
The dict equivalent of the above might then be something along the
lines of {**v for v in dict_of_dicts.values()}. In case the values
(which be all dicts) are records with the same keys, one might go and
prepend the keys with their former keys using
{
    **dict(
        ("{}_{}".format(k, k_sub), v_sub)
        for k_sub, v_sub in v.items()
    ) for k, v in dict_of_dicts.items()
}
Was anyone able to follow me through this?

cheers!
mar77i


More information about the Python-ideas mailing list