Re: get_used_memory

Jon Ribbens <[email protected]> Mon, 15 Jun 2026 00:40:11 -0000 (UTC)
Newsgroups comp.lang.python
Organization A noiseless patient Spider
Message-ID <[email protected]>
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.