For a while we have been able to use the unpacking syntax when building iterables:
a = [1, 2, 3]
b = [4, 5, 6]
[*a, *b] # [1, 2, 3, 4]
{*a, *b} # {1, 2, 3, 4}
A common pattern when refactoring code is to transform simple loops into comprehensions:
Flight = namedtuple("Flight", ["departure", "arrival"])
f1 = Flight("PAR", "NYC")
f2 = Flight("LON", "MAD")
flights = [f1, f2]
# Not so good
points = []
for f in flights:
points.append(f.departure)
# Better
points = [f.departure for f in flights]
But in the case where you need to extract more than one value in each loop iteration, this no longer works as well:
points = []
for f in flights:
points += (f.departure, f.arrival)
# Without relying in itertools.chain, no way to make this into a comprehension
points = list(chain.from_iterable((f.departure, f.arrival) for f in flights))
I was thinking that we could update the syntax to allow for:
points = [*(f.departure, f.arrival) for f in flights]
The unpacking nicely mirrors the [*a, *b]
syntax where you expand one iterable.
As this is currently a syntax error, I believe this would be backward compatible, but I might have missed something.
Edit: sorry, just saw Why can't iterable unpacking be used in comprehension? which looks close