On Mon, 15 Jun 2026 at 14:27, Jon Ribbens via Python-list <[email protected]> wrote: > > On 2026-06-15, Paul Rubin <[email protected]> wrote: > > Jon Ribbens <[email protected]> writes: > >> {e[0][:-1]: int(e[1]) for x in xs if (e := x.split())} > > > > Does this work? > > > > { a : int(b)) for x in xs if (a,b := x.split()) } > > No, for three reasons. Firstly, the lines with units result in x.split() > having 3 members, so you can't assign it to a 2-tuple. Secondly, it > appears that (a, b := x) means "create a tuple whose first member is > a and whose second member is x, and also assign x to b", which is not > at all what we need. Thirdly, if we fix that second one by saying > ((a, b) := x.split()), you get "SyntaxError: cannot use assignment > expressions with tuple". (Assignment expressions are weirdly limited > for no apparent reason.) >
Probably because they're not supposed to be used like this. I mean, let's be honest here, it's already abusing the 'if' clause on a comprehension for something that isn't really conditional at all. Not everything should be written as a comprehension. ChrisA -- https://mail.python.org/mailman3//lists/python-list.python.org
