Menu

Consistency of new unpacking syntax in 3.15?

#python#redditpython#else#expression#versions
Reading 0:00
15s threshold

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

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.