To even better justify the new syntax over existing approaches, I think we can further generalize unpacking in comprehensions by allowing *
and **
to unpack any arbitrary expression within a comprehension, not just the outermost expression.
So that as an example to flatten [1, 2, [3, 4]]
into [1, 2, 3, 4]
:
[*x if isinstance(x, Iterable) else x for x in its]
would be equivalent to:
[x for it in its for x in (it if isinstance(it, Iterable) else [it])]
This can potentially be done by making *
and **
true unary operators that wrap the operand in a new Unpackable
object for the yielder of a comprehension to unwrap and unpack.