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

Sven R. Kunze srkunze at mail.de
Wed Oct 12 09:58:25 EDT 2016


Hi Martti,

On 11.10.2016 14:42, Martti Kühne wrote:
> 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?

Reading PEP448 it seems to me that it's already been considered: 
https://www.python.org/dev/peps/pep-0448/#variations

The reason for not-inclusion were about concerns about acceptance 
because of "strong concerns about readability" but also received "mild 
support". I think your post strengthens the support given that you 
"expected it to just work". This shows at least to me that the concerns 
about readability/understandability are not justified much.

Personally, I find inclusion of */** expansion for comprehensions very 
natural. It would again strengthen the meaning of */** for unpacking 
which I am also in favor of.

Cheers,
Sven


More information about the Python-ideas mailing list