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.)
> I think it's ok to keep the ':' in the field name. Otherwise
>
> { a.rstrip(':') : int(b)) for x in xs if (a,b := x.split()) }
>
> seems a bit more explicit at the expense of a few more chars.
a[:-1] would work and is shorter. But yeah it doesn't matter much.
--
https://mail.python.org/mailman3//lists/python-list.python.org