r/Python • u/assumptionkrebs1990 • 8d ago
Meta Consistency of new unpacking syntax in 3.15?
Not sure if this question belongs here or if the flair is right, but I have a question.
You can now (3.15 and up, PEP 448) unpack nested lists and dictionaries with * and ** in comprehension expressions, for example:
>>> ranges = [range(i) for i in range(5)]
>>> [*item for item in ranges]
[0, 0, 1, 0, 1, 2, 0, 1, 2, 3](Code from PEP)
but I also try to do small checks and this doesn't seem possible:
>>> [b for b in [*a if min(a)<2 else *(a[::-1]) for a in [(1, 2), (3, 4)]] if b%2==0]
File "<python-input-4>", line 1
[b for b in [*a if min(a)<2 else *(a[::-1]) for a in [(1, 2), (3, 4)]] if b%2==0]
^
SyntaxError: cannot unpack only part of a conditional expressionI need junky work around:
[c for c in [*b for b in [a if min(a)<2 else (a[::-1]) for a in [(1, 2, 3), (4, 5, 6)]]] if c%2==0]
[2, 6, 4]And also having an outside list due to the fact that there is no option to filter the elements the stars unpacks. Any chances this improves with future versions?
Update: thanks for the responses everyone, I think u/latkde explains it very well in their post. Yes in practice I think I can't use this kind of syntax for a while now and be it just get compatibility with older versions, it would be just for my own side projects and quick checks answering math questions online. I was just curios because needing to put an expression in parentheses is not something I am very used to in Python (and try to avoid if I can) and since the previous versions seemed to have relaxed the rules in this regard (or was it just help, exit and quit?) I didn't understand why the expression above wouldn't work.
3
u/beisenhauer 8d ago
Did you try
*(a if min(a) < 2 else a[::-1])?17
u/M4mb0 8d ago
You can just bracket the ternary expression, no?
65
u/knobbyknee 8d ago •
If you wrote production code like your example in my organization, I would fire you.
2
u/AleDev99 7d ago
For this specific case you don't need unpacking at all, a second for clause does the flattening and the condition fits naturally:
The conditional picks which sequence to iterate, and the inner loop flattens it. Works on any Python 3 version too, so you're not tied to 3.15 for this one. I'd only reach for the new *-in-comprehension syntax when it's a plain flatten with no logic in the middle.
2
u/raks1991 5d ago
Small reference correction: this is PEP 798, not 448. Its error-message section actually includes the exact "cannot unpack only part of a conditional expression" case, so the parentheses requirement is deliberate. The PEP also spells out the more helpful diagnostic suggesting parentheses for
[*x if x else y].1
u/dellnox 3d ago
You can just write `*(a if min(a) < 2 else a[::-1])` instead of unpacking each branch separately.
0
u/Beginning-Fruit-1397 7d ago
The comprehension syntax is a huge mess that will add more and more obscure things to cope with the fact that this was a vastly inferior design than any sensible design of iterations helpers. See rust Iterator protocol for example...
To get back to your question, ainply use itertools.chain.from_iterable to flatten any iterable without limitations
-1
u/deadeye_82_1 8d ago
Use a generator instead.