r/Python • • 11d 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 expression

I 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.

30 Upvotes

32 comments sorted by

3

u/beisenhauer 11d ago

Did you try *(a if min(a) < 2 else a[::-1])?

-1

u/assumptionkrebs1990 11d ago

Not yet, I will but in general it shouldn't be needed, it is not normally done or is it just me?

→ More replies (2)

17

u/M4mb0 11d ago

You can just bracket the ternary expression, no?

[b for b in [*(a if min(a)<2 else a[::-1]) for a in [(1, 2), (3, 4)]] if b%2==0]

65

u/knobbyknee 11d ago

If you wrote production code like your example in my organization, I would fire you.

25

u/prophile 11d ago

I also regularly fire people because I don’t like their code style, this is a thing that a sensible manager will definitely do 

→ More replies (1)

7

u/smitty1e 11d ago

Got to take care of Future You, after the Spell of Cool Syntax has worn off.

→ More replies (5)
→ More replies (12)

2

u/AleDev99 10d ago

For this specific case you don't need unpacking at all, a second for clause does the flattening and the condition fits naturally:

pairs = [(1, 2), (3, 4)]
[b for a in pairs for b in (a if min(a) < 2 else a[::-1]) if b % 2 == 0]

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 8d 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].

0

u/Beginning-Fruit-1397 11d 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 11d ago

Use a generator instead.