PEP 798: Unpacking in Comprehensions

I think that this extension is out of scope for this PEP (which is specifically about extending the syntax for comprehensions rather than introducing another shorthand in their place).

I’m also a little unsure of how to interpret the suggested code in terms of more basic operations. [*things] has a clear, established meaning, but [*(*iterables)] doesn’t (it’s a syntax error currently); if it were allowed, though, this kind of syntax feels to me like it’s trying to say: unpack iterables into a generator/tuple, and then re-unpack that generator/tuple into a list. That is, if this were valid syntax, it looks like there are two unpackings there, and both of them on iterables (with nothing in the syntax there that explicitly suggests unpacking the elements inside of iterables).

I unfortunately think that adding this syntax would muddy the waters a little bit in terms of the current PEP, so I don’t think it should be included here; if we do want to consider that, it should be in a later proposal (though I’ll say I’m personally not in favor of extending things in that way).

12 Likes