Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

If it wasn't for Newton, we wouldn't have to eat bruised apples.


devel / comp.lang.python / RE: Popping key causes dict derived from object to revert to object

SubjectAuthor
* Popping key causes dict derived from object to revert to objectLoris Bennett
+- Re: Popping key causes dict derived from object to revert to objectdieter.maurer
`* Re: Popping key causes dict derived from object to revert to objectMark Bourne
 `* Re: Popping key causes dict derived from object to revert to objectLoris Bennett
  +* Re: Popping key causes dict derived from object to revert to objectGrant Edwards
  |`* Re: Popping key causes dict derived from object to revert to objectLoris Bennett
  | +* Re: Popping key causes dict derived from object to revert to objectMichael F. Stemper
  | |`* Re: Popping key causes dict derived from object to revert to objectLoris Bennett
  | | `- Re: Popping key causes dict derived from object to revert to objectJon Ribbens
  | +- Re: Popping key causes dict derived from object to revert to objectGrant Edwards
  | `- RE: Popping key causes dict derived from object to revert to object<avi.e.gross
  +- RE: Popping key causes dict derived from object to revert to object<avi.e.gross
  +- Re: Popping key causes dict derived from object to revert to objectLoris Bennett
  `- RE: Popping key causes dict derived from object to revert to object<avi.e.gross

1
Popping key causes dict derived from object to revert to object

<87zfurgb6t.fsf@zedat.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29699&group=comp.lang.python#29699

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.bennett@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Popping key causes dict derived from object to revert to object
Date: Thu, 21 Mar 2024 10:56:42 +0100
Organization: FUB-IT (ex-ZEDAT), Freie Universität Berlin
Lines: 43
Message-ID: <87zfurgb6t.fsf@zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de kIOnY/0Saz6HAeybLSo4HAdpyhZEJDN+gDGkYqe7u0VG5u
Cancel-Lock: sha1:vdhdq2g48/xaVXAQwZxWVjapvL4= sha1:XvDflDrTv3S9GWA3pGhBG+Dt6Xc= sha256:Ly34mhih+nhD1L9nbL8NBWNVWzjSqgh7NOl+uvu8Ldc=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
 by: Loris Bennett - Thu, 21 Mar 2024 09:56 UTC

Hi,

I am using SQLAlchemy to extract some rows from a table of 'events'.
From the call to the DB I get a list of objects of the type

sqlalchemy.orm.state.InstanceState

I would like to print these rows to the terminal using the 'tabulate'
package, the documentation for which says

The module provides just one function, tabulate, which takes a list of
lists or another tabular data type as the first argument, and outputs
a nicely formatted plain-text table

So as I understand it, I need to convert the InstanceState-objects to,
say, dicts, in order to print them. However I also want to remove one
of the keys from the output and assumed I could just pop it off each
event dict, thus:

event_dicts = [vars(e) for e in events]
print(type(event_dicts[0]))
event_dicts = [e.pop('_sa_instance_state', None) for e in event_dicts]
print(type(event_dicts[0]))

However, this prints

<class 'dict'>
<class 'sqlalchemy.orm.state.InstanceState'>

If I comment out the third line, which pops the unwanted key, I get

<class 'dict'>
<class 'dict'>

Why does popping one of the keys cause the elements of the list to
revert back to their original class?

Cheers,

Loris

--
This signature is currently under constuction.

Re: Popping key causes dict derived from object to revert to object

<mailman.0.1711038451.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29703&group=comp.lang.python#29703

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: dieter.maurer@online.de
Newsgroups: comp.lang.python
Subject: Re: Popping key causes dict derived from object to revert to object
Date: Thu, 21 Mar 2024 17:21:52 +0100
Lines: 28
Message-ID: <mailman.0.1711038451.3468.python-list@python.org>
References: <87zfurgb6t.fsf@zedat.fu-berlin.de>
<26108.24224.409498.22809@ixdm.fritz.box>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de mLy50v4rhUoeH5P3N03fQQF2edXEvTvVFdwEdkB2A+Ig==
Cancel-Lock: sha1:BooraTJNQTAjKzTOBZBiCVBSSDo= sha256:4PmHi6PXqCo9vm8TWHwTeJUFmKiDyQoeOI/dZkNKoQ8=
Return-Path: <dieter.maurer@online.de>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=online.de header.i=dieter.maurer@online.de
header.b=NyaeUvDh; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.066
X-Spam-Evidence: '*H*': 0.88; '*S*': 0.01; 'cc:addr:python-list':
0.09; 'prints': 0.09; 'skip:[ 20': 0.09; 'skip:` 10': 0.09;
'typically': 0.09; 'cc:no real name:2**0': 0.14; 'bennett': 0.16;
'key,': 0.16; 'none)': 0.16; 'received:217.72.192.75': 0.16;
'problem': 0.16; 'cc:addr:python.org': 0.20; 'returns': 0.22;
'received:de': 0.23; 'cc:2**0': 0.25; 'output': 0.28; 'it,': 0.29;
'comment': 0.31; 'received:kundenserver.de': 0.32;
'received:mout.kundenserver.de': 0.32; 'but': 0.32; 'header:In-
Reply-To:1': 0.34; 'change': 0.36; 'subject:from': 0.37; 'could':
0.38; 'wrote': 0.39; 'want': 0.40; 'likely': 0.61; 'skip:i 20':
0.62; 'key': 0.64; 'received:217': 0.67; 'order': 0.69;
'received:80': 0.70; 'thus:': 0.84; 'unwanted': 0.84; 'pop': 0.91;
'removal': 0.91; 'line,': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=online.de;
s=s42582890; t=1711038449; x=1711643249; i=dieter.maurer@online.de;
bh=vbKpfai+YIHLbVXj6nlfH11iJgWOhQR2VAdu/SwKgJc=;
h=X-UI-Sender-Class:Date:From:To:Cc:Subject:In-Reply-To:
References;
b=NyaeUvDhCC8fOBroinV0LVz0MqEAK6aN+fp7HhtcIX6dk+SNbxOHnhIO2RYJplKZ
DwlDt3kZ03j+FamvdqgKAUilfP95qbPCWH+zYazNc9OqEmjR5AGbvYC2VlDo2hq6k
ICPqUBeAiPvsOjX9uHSPJL/D5Lvr6iMqDZIGRJQPRl5L6PiDJbPl+BzxfSWTgaf9l
andvkM9p2+Y0GY7jGm8bwGmgeV7RffwKNDNfIiX+0DrK3rb4bi4RpUaNX13nqVK3g
mWGEUJA4bXm+PfzcScC5/r6aKMsP1y7trU07eYORGYBYI1TEjpHb+EiOyBXIlDi+g
iOysYitiFTolp9yzQw==
X-UI-Sender-Class: 6003b46c-3fee-4677-9b8b-2b628d989298
In-Reply-To: <87zfurgb6t.fsf@zedat.fu-berlin.de>
X-Mailer: VM 8.0.12-devo-585 under 21.4 (patch 24) "Standard C" XEmacs Lucid
(x86_64-linux-gnu)
X-Provags-ID: V03:K1:doj4B+V2sw6YhTCjKCYbeLJk5kN6jlhFiSvXdwpi/AVoB2q6vGV
g6LtlvaqkRQQe7ZBo+ZCz3uGLBbhBy8fNqQeRP+rs30vOvsyVBglH9Dj4Zv7BotT3hI0Tlc
xyVqp4F71bGc7IgH5ERrmI1X2YUMPm3oEop4SXlFR6fmE67Z16QDreM6hDD+uEEQYtc375K
c5mKBO51zSSkbGDvCrxdQ==
X-Spam-Flag: NO
UI-OutboundReport: notjunk:1;M01:P0:C2crIHeov2I=;XMnXAuxSSODIvY7+Gksvn3qSdQg
FiSRLaoUZhSAwgYLbpNXGwGsZ90voOYPrwUOQgMaqz4CM3ZNYR96jG9D72PdLH2EEW89BTAgR
F7rBzKj2Koi12odn/g+yO7bDq2wAyFNJ0zNjtejpbftQLUtEG4GSZlHBQs1PGPIB/vIhpZn1i
wB8HevO2IAY09EUaefaUQBpxQka3mBJoQBkMyaukSF6uGKIAko4pFRnpl4N7o+7aXHh1xmdoQ
L86aWsY0gOI7H+RPAvT2/Q8JMlwBA4QZzPY9dKQLHqU9gym+aAP2V/DvGZQgWa6jVjKeD4396
CcgpNW8i4F+Yuy5vPQPFcqiFSZ26c84YTTnwMk0CbtVjQCUvk4mMMvRBDKz1FzXGMvu3rkAZm
nlr4iSCCnmMm55LdznoWNx4wrUHAO7Y5OmWFzw45/OUHcIPFZ0RhvM2HAY4p9C6P0YB0Wgqq3
QmntoP07jlzB+aQymsdjyy2DjtkWL4KPXT7AbjcW+BQXxzZFDo0PGvf4doD7lIwEaYyghDA46
CzzT6POkgAFViDn3u+7GJrFG8rPO9F6/FybBx8fUEjd6X4SGaEomTyEMdZ/LJ2Txf1qAxZu83
cb4Cf39q0OBgSfZNycRWyiedzAWXQFhRyMqCVgvlE48LA9PjSGz5icoGRuwsHK68McWz+v4qP
Hei1P8uLMQrBoRnDHQ6N0cIAQadT6cgjSXvcdvGfCFKcEKYqyXaiNJyLjHB/UnQW8a16TD4rg
VHpYjcsBmPcSno/npRKvVCPSE5R3CEI7qSalUBVwOzJwzq/Ajx4h+A=
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <26108.24224.409498.22809@ixdm.fritz.box>
X-Mailman-Original-References: <87zfurgb6t.fsf@zedat.fu-berlin.de>
 by: dieter.maurer@online.de - Thu, 21 Mar 2024 16:21 UTC

Loris Bennett wrote at 2024-3-21 10:56 +0100:
> ...
>So as I understand it, I need to convert the InstanceState-objects to,
>say, dicts, in order to print them. However I also want to remove one
>of the keys from the output and assumed I could just pop it off each
>event dict, thus:
>
> event_dicts = [vars(e) for e in events]
> print(type(event_dicts[0]))
> event_dicts = [e.pop('_sa_instance_state', None) for e in event_dicts]
> print(type(event_dicts[0]))
>
>However, this prints
>
> <class 'dict'>

`vars` typically returns a `dict`.

> <class 'sqlalchemy.orm.state.InstanceState'>
This is what you have popped.
>
>If I comment out the third line, which pops the unwanted key, I get
Then you do not change `event_dicts`.

You problem likely is:
`pop` does not return the `dict` after the removal of a key
but the removed value.

Re: Popping key causes dict derived from object to revert to object

<uti7n1$2ef92$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29709&group=comp.lang.python#29709

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nntp.mbourne@spamgourmet.com (Mark Bourne)
Newsgroups: comp.lang.python
Subject: Re: Popping key causes dict derived from object to revert to object
Date: Thu, 21 Mar 2024 21:09:53 +0000
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <uti7n1$2ef92$1@dont-email.me>
References: <87zfurgb6t.fsf@zedat.fu-berlin.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Mar 2024 21:09:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e29d700c3c650c2f8b31878382de2e99";
logging-data="2571554"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18bLhpF/rqU1jembenFCli+"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
SeaMonkey/2.53.18.1
Cancel-Lock: sha1:fbz5PU1cclYuIrEzDvAkQND08SQ=
In-Reply-To: <87zfurgb6t.fsf@zedat.fu-berlin.de>
 by: Mark Bourne - Thu, 21 Mar 2024 21:09 UTC

Loris Bennett wrote:
> Hi,
>
> I am using SQLAlchemy to extract some rows from a table of 'events'.
> From the call to the DB I get a list of objects of the type
>
> sqlalchemy.orm.state.InstanceState
>
> I would like to print these rows to the terminal using the 'tabulate'
> package, the documentation for which says
>
> The module provides just one function, tabulate, which takes a list of
> lists or another tabular data type as the first argument, and outputs
> a nicely formatted plain-text table
>
> So as I understand it, I need to convert the InstanceState-objects to,
> say, dicts, in order to print them. However I also want to remove one
> of the keys from the output and assumed I could just pop it off each
> event dict, thus:
>
> event_dicts = [vars(e) for e in events]
> print(type(event_dicts[0]))
> event_dicts = [e.pop('_sa_instance_state', None) for e in event_dicts]
> print(type(event_dicts[0]))

vars() returns the __dict__ attribute of the object. It may not be a
good idea to modify that dictionary directly (it will also affect the
object), although it might be OK if you're not going to do anything else
with the original objects. To be safer, you could copy the event objects:
event_dicts = [dict(vars(e)) for e in events]
or:
event_dicts = [vars(e).copy()]

> However, this prints
>
> <class 'dict'>
> <class 'sqlalchemy.orm.state.InstanceState'>
>
> If I comment out the third line, which pops the unwanted key, I get
>
> <class 'dict'>
> <class 'dict'>
>
> Why does popping one of the keys cause the elements of the list to
> revert back to their original class?

As Dieter pointed out, the main problem here is that pop() returns the
value removed, not the dictionary with the rest of the values. You
probably want something more like:
for e in event_dicts:
del e['_sa_instance_state']
(There's not really any point popping the value if you're not going to
do anything with it - just delete the key from the dictionary)

--
Mark.

Re: Popping key causes dict derived from object to revert to object

<87plvmr93v.fsf@zedat.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29713&group=comp.lang.python#29713

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.bennett@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Re: Popping key causes dict derived from object to revert to object
Date: Fri, 22 Mar 2024 08:58:28 +0100
Organization: FUB-IT (ex-ZEDAT), Freie Universität Berlin
Lines: 66
Message-ID: <87plvmr93v.fsf@zedat.fu-berlin.de>
References: <87zfurgb6t.fsf@zedat.fu-berlin.de> <uti7n1$2ef92$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de ruXiBQ1AuS5fgUfBQ4URHAN+ahD+g0rsfcWW5ySDHG2zwb
Cancel-Lock: sha1:ZKPJSWgoLswlujc4lykuqNYRVPk= sha1:lcN9iqs9naGnkUd6A1VL5W8bGT4= sha256:YJ90HHsXm6mckI+qpEupBhMC9ABSC+Kgaem4yxzxmDI=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
 by: Loris Bennett - Fri, 22 Mar 2024 07:58 UTC

Mark Bourne <nntp.mbourne@spamgourmet.com> writes:

> Loris Bennett wrote:
>> Hi,
>> I am using SQLAlchemy to extract some rows from a table of 'events'.
>> From the call to the DB I get a list of objects of the type
>> sqlalchemy.orm.state.InstanceState
>> I would like to print these rows to the terminal using the
>> 'tabulate'
>> package, the documentation for which says
>> The module provides just one function, tabulate, which takes a
>> list of
>> lists or another tabular data type as the first argument, and outputs
>> a nicely formatted plain-text table
>> So as I understand it, I need to convert the InstanceState-objects
>> to,
>> say, dicts, in order to print them. However I also want to remove one
>> of the keys from the output and assumed I could just pop it off each
>> event dict, thus:
>> event_dicts = [vars(e) for e in events]
>> print(type(event_dicts[0]))
>> event_dicts = [e.pop('_sa_instance_state', None) for e in event_dicts]
>> print(type(event_dicts[0]))
>
> vars() returns the __dict__ attribute of the object. It may not be a
> good idea to modify that dictionary directly (it will also affect the
> object), although it might be OK if you're not going to do anything
> else with the original objects. To be safer, you could copy the event
> objects:
> event_dicts = [dict(vars(e)) for e in events]
> or:
> event_dicts = [vars(e).copy()]

Thanks for making this clear to me. However, in the end I actually
decided to use the list comprehension without either 'dict()' or
'vars(). Instead I just select the keys I want and so don't need to pop
the unwanted key later and can simultaneously tweak the names of the
key for better printing to the terminal.

>> However, this prints
>> <class 'dict'>
>> <class 'sqlalchemy.orm.state.InstanceState'>
>> If I comment out the third line, which pops the unwanted key, I get
>> <class 'dict'>
>> <class 'dict'>
>> Why does popping one of the keys cause the elements of the list to
>> revert back to their original class?
>
> As Dieter pointed out, the main problem here is that pop() returns the
> value removed, not the dictionary with the rest of the values. You
> probably want something more like:
> for e in event_dicts:
> del e['_sa_instance_state']
> (There's not really any point popping the value if you're not going to
> do anything with it - just delete the key from the dictionary)

Yes, I was mistakenly thinking that the popping the element would leave
me with the dict minus the popped key-value pair. Seem like there is no
such function.

Cheers,

Loris
--
This signature is currently under constuction.

Re: Popping key causes dict derived from object to revert to object

<mailman.13.1711123277.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29721&group=comp.lang.python#29721

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: grant.b.edwards@gmail.com (Grant Edwards)
Newsgroups: comp.lang.python
Subject: Re: Popping key causes dict derived from object to revert to object
Date: Fri, 22 Mar 2024 12:01:15 -0400 (EDT)
Lines: 36
Message-ID: <mailman.13.1711123277.3468.python-list@python.org>
References: <87zfurgb6t.fsf@zedat.fu-berlin.de>
<uti7n1$2ef92$1@dont-email.me> <87plvmr93v.fsf@zedat.fu-berlin.de>
<4V1Rrq72BnznWJ6@mail.python.org>
X-Trace: news.uni-berlin.de 0Cz1XGNZ3+SFYpqPkDsd6gg038wKC2AL641wx6/RPHeA==
Cancel-Lock: sha1:I8iLMgxR63lc2GnfxMFk2/W9tJw= sha256:V8cAmpWbeFkykmBDBMiqkJ4Hb9DzNZOhLqP+MqJoO3o=
Return-Path: <grant.b.edwards@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.029
X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'is.': 0.05; 'expression':
0.09; 'minus': 0.09; 'bennett': 0.16; 'cases,': 0.16; 'dict':
0.16; 'from:addr:grant.b.edwards': 0.16; 'from:name:grant
edwards': 0.16; 'key-value': 0.16; 'wrote:': 0.16; 'to:addr
:python-list': 0.20; 'leave': 0.27; '>>>': 0.28; 'thinking': 0.28;
'printed': 0.28; 'header:User-Agent:1': 0.30; 'seem': 0.31;
'deleted': 0.32; 'python-list': 0.32; 'there': 0.33; 'yes,': 0.35;
'from:addr:gmail.com': 0.35; 'subject:from': 0.37; 'both': 0.40;
'method': 0.61; 'message-id:invalid': 0.68; 'del': 0.70;
'returned': 0.81; 'left': 0.83; 'pop': 0.91
User-Agent: slrn/1.0.3 (Linux)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <4V1Rrq72BnznWJ6@mail.python.org>
X-Mailman-Original-References: <87zfurgb6t.fsf@zedat.fu-berlin.de>
<uti7n1$2ef92$1@dont-email.me> <87plvmr93v.fsf@zedat.fu-berlin.de>
 by: Grant Edwards - Fri, 22 Mar 2024 16:01 UTC

On 2024-03-22, Loris Bennett via Python-list <python-list@python.org> wrote:

> Yes, I was mistakenly thinking that the popping the element would
> leave me with the dict minus the popped key-value pair.

It does.

> Seem like there is no such function.

Yes, there is. You can do that with either pop or del:

>>> d = {'a':1, 'b':2, 'c':3}
>>> d
{'a': 1, 'b': 2, 'c': 3}
>>> d.pop('b')
2
>>> d
{'a': 1, 'c': 3}

>>> d = {'a':1, 'b':2, 'c':3}
>>> del d['b']
>>> d
{'a': 1, 'c': 3}

In both cases, you're left with the dict minus the key/value pair.

In the first case, the deleted value printed by the REPL because it
was returned by the expression "d.pop('b')" (a method call).

In the second case is no value shown by the REPL because "del d['b']"
is a statement not an expression.

RE: Popping key causes dict derived from object to revert to object

<mailman.16.1711131063.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29724&group=comp.lang.python#29724

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: <avi.e.gross@gmail.com>
Newsgroups: comp.lang.python
Subject: RE: Popping key causes dict derived from object to revert to object
Date: Fri, 22 Mar 2024 14:11:00 -0400
Lines: 26
Message-ID: <mailman.16.1711131063.3468.python-list@python.org>
References: <87zfurgb6t.fsf@zedat.fu-berlin.de> <uti7n1$2ef92$1@dont-email.me>
<87plvmr93v.fsf@zedat.fu-berlin.de>
<005a01da7c84$4b9ba5f0$e2d2f1d0$@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de kD3j3cQjJDOztQBjJZwGCwPX9xVvRfWJpTGxiYKWXHAA==
Cancel-Lock: sha1:Of9zgaVaAzu7suxWETaqX/S+H2g= sha256:5egGY5jP7dD7OxFST1P/EMMtsTPIYBuj8Y5rLGBx28o=
Return-Path: <avi.e.gross@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=mI7ScEAD;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.013
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'def': 0.04; 'minus': 0.09;
'received:108': 0.09; 'trivial': 0.09; 'dict': 0.16; 'key-value':
0.16; 'method?': 0.16; 'odd': 0.16; 'variant': 0.16; 'wrote:':
0.16; 'message-id:@gmail.com': 0.18; 'to:addr:python-list': 0.20;
'tried': 0.26; 'leave': 0.27; '>>>': 0.28; 'thinking': 0.28;
'seem': 0.31; 'concept': 0.32; 'but': 0.32; 'there': 0.33; 'able':
0.34; 'header:In-Reply-To:1': 0.34; 'received:google.com': 0.34;
'item': 0.35; 'from:addr:gmail.com': 0.35; 'subject:from': 0.37;
'received:209.85': 0.37; 'others': 0.37; 'received:209': 0.39;
'adding': 0.39; 'use': 0.39; 'received:209.85.222': 0.39;
'explain': 0.40; 'want': 0.40; 'your': 0.64; 'similar': 0.65;
'del': 0.70
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1711131061; x=1711735861; darn=python.org;
h=thread-index:content-language:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:to:from
:from:to:cc:subject:date:message-id:reply-to;
bh=zOz/KX8gsrD5I89b7nJ60tb2HJnEh+ZceQnsuccHDk8=;
b=mI7ScEAD5i+eHh4aMzQHogXaRNw0PU/YE9RfvmpiIwo/AAQSraNPh7kt+JgZSSGwGG
7u2skWTzRTIQTcmmnwnRZKd/00semR0wSTg722JzrESumtOmVhN5BF4bAC99RGIwcGMt
jTklSA19rDiko3khsjZKzE1SukA9Xeb7GhWvzEpG9wQJBFu/bCja9sBI4Wfj0s/Y6HkR
zFsBoFQr+/3hObGpnFzs92JEl4wdzpPjzlp2I3OkC4/VfQYIELlN21QsP7EnjVPePnSp
iJmTlVBkqgb/EDacLcibU5+ibptKBqqhm+DschOjxS8VhfGMIGTxcOEnnoWjWPGO50s/
v01Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1711131061; x=1711735861;
h=thread-index:content-language:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:to:from
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=zOz/KX8gsrD5I89b7nJ60tb2HJnEh+ZceQnsuccHDk8=;
b=eBL5OXAJMhrhThYOF5fqeAeEel+XXePQzLMYo4P5uHCsHTtNoX/8gdWjqwbhQQrTdh
XCZWAUZj84/yZT3AK5Vl2VmQHwtz01zLMfTlMF5/RnvuRItNSuo4GWWHwoIuKjMOjXSb
FYyPpG0Tn3aXA9RBwqh2bna5wvtzoiMVWz/nqseYrZ+BD+o1cxtnlgDnGhlZGjFTPIOE
c400iX9tgN7nAz7b6sXrZNsC3hKOs5yZ3VKupgp5Gd06jBOJm/0wvFOwMqEZ/W9vAvYy
bO6vyM7mR5GqULcndlcGxM7qd6MVJaPidwAKj00sHgy/Ye6SiI0X7N3D1gxTnxWKiiAa
CSyA==
X-Forwarded-Encrypted: i=1;
AJvYcCU7zmWp4fFJrDYWqYEI8eNhjfW4PJKoKfJT+wsUin0vBYbdDNaHAbunLMJgKvRCfw/blrRjoGbZZBMEtXklV24YphnNXyHD
X-Gm-Message-State: AOJu0Yw2HsWSXsUFElbepd6mAByxAN3cAcLIgNRt9mRBgwp8R9PmbcFd
kj+7BSh8F0rgHlYAEP3dg3NsYdsurY6G6WKrrvqCT7gkLf3GL5M5ARQHh4+c
X-Google-Smtp-Source: AGHT+IEwACass8gvJ3AEye1ZqEoIjQ3fbvq9k9Xb/s+3Ia005OvLzBINxz+DNjItkV7T9cNJhb6f4g==
X-Received: by 2002:a05:620a:b96:b0:788:7622:ceb2 with SMTP id
k22-20020a05620a0b9600b007887622ceb2mr516827qkh.18.1711131061473;
Fri, 22 Mar 2024 11:11:01 -0700 (PDT)
In-Reply-To: <87plvmr93v.fsf@zedat.fu-berlin.de>
X-Mailer: Microsoft Outlook 16.0
Content-Language: en-us
Thread-Index: AQKM7iLteXOq+KoZ2KtNA4Zjj8pI1wKmK1mHAVVm8Jqvv01YYA==
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <005a01da7c84$4b9ba5f0$e2d2f1d0$@gmail.com>
X-Mailman-Original-References: <87zfurgb6t.fsf@zedat.fu-berlin.de>
<uti7n1$2ef92$1@dont-email.me> <87plvmr93v.fsf@zedat.fu-berlin.de>
 by: <avi.e.gross@gmail.com> - Fri, 22 Mar 2024 18:11 UTC

Loris wrote:

"Yes, I was mistakenly thinking that the popping the element would leave
me with the dict minus the popped key-value pair. Seem like there is no
such function."

Others have tried to explain and pointed out you can del and then use the
changed dict.

But consider the odd concept of writing your own trivial function.

def remaining(adict, anitem):
_ = adict.pop(anitem)
# alternatively duse del on dict and item
return adict

>>> remaining({"first": 1, "second": 2, "third": 3}, "second")
{'first': 1, 'third': 3}

Or do you want to be able to call it as in dict.remaining(key) by
subclassing your own variant of dict and adding a similar method?

Re: Popping key causes dict derived from object to revert to object

<mailman.23.1711349357.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29733&group=comp.lang.python#29733

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.bennett@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Re: Popping key causes dict derived from object to revert to object
Date: Mon, 25 Mar 2024 07:45:15 +0100
Organization: FUB-IT (ex-ZEDAT), Freie Universität Berlin
Lines: 36
Message-ID: <mailman.23.1711349357.3468.python-list@python.org>
References: <87zfurgb6t.fsf@zedat.fu-berlin.de> <uti7n1$2ef92$1@dont-email.me>
<87plvmr93v.fsf@zedat.fu-berlin.de>
<005a01da7c84$4b9ba5f0$e2d2f1d0$@gmail.com>
<874jcustc4.fsf@zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de I+RU9l+giKZNIimBFouvSwIvG/pNk511QhPXHVRs7CQg==
Cancel-Lock: sha1:JIPfoS7/ykjVxUfO6JMkYQst1qM= sha256:boAbHQht5vO9Zm7xoHTiyZQNNdp0/Ppmmv4aWzUw2OM=
Return-Path: <loris@zedat.fu-berlin.de>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=fu-berlin.de header.i=@fu-berlin.de header.b=UvB+lj89;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; ':-)': 0.09;
'cc:addr:python-list': 0.09; 'minus': 0.09; 'trivial': 0.09;
'writes:': 0.09; 'cheers,': 0.11; 'cc:no real name:2**0': 0.14;
'>>>>': 0.16; 'dict': 0.16; 'indeed': 0.16; 'key-value': 0.16;
'method?': 0.16; 'odd': 0.16; 'variant': 0.16; 'wrote:': 0.16;
'cc:addr:python.org': 0.20; 'received:de': 0.23; 'cc:2**0': 0.25;
'tried': 0.26; 'leave': 0.27; 'else': 0.27; 'thinking': 0.28;
'header:User-Agent:1': 0.30; 'seem': 0.31;
'header:Organization:1': 0.31; 'concept': 0.32; 'but': 0.32;
'there': 0.33; 'able': 0.34; 'header:In-Reply-To:1': 0.34; 'item':
0.35; 'currently': 0.37; 'subject:from': 0.37; 'others': 0.37;
'adding': 0.39; 'use': 0.39; 'explain': 0.40; 'something': 0.40;
'want': 0.40; 'your': 0.64; 'similar': 0.65; 'decided': 0.67;
'del': 0.70; 'signature': 0.76
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed;
d=fu-berlin.de; s=fub01; h=Content-Type:MIME-Version:Message-ID:In-Reply-To:
Date:References:Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding:
Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender:
Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:
List-Subscribe:List-Post:List-Owner:List-Archive;
bh=Hf/4L/p+O1EMod12T6vKf3z6N9g4jc4whAN+gUxS4Y8=; t=1711349355; x=1711954155;
b=UvB+lj899w9rweXRBMPUR9xutxZZn20jdMHV1vy8mSqu0ZapusixIK0nTKCkTZmKYS0aPBC8cTs
efYCQowEv4GHs8gpuW/9UTdz68L4mGbpho9VQqpEbn2P1dWYo/Hi66mo3Z6IlVyf2j14p/7nfr/Nr
CMGtFZR0Nq0YzDE2T8yPa75CB3GSK06H1Re+gwt3XdFVZY8RF5qVr6egY7WzF0YrkQbppCq6ZswGD
Rlc7IsbHtGK9bcZkHyRrXM4+LLOYYM0plG9cZeS6VU5NVl1UoBbIqkGL0I6vGjPie/HovY03//do2
XhEvR4Yq48eXYpcRsUDbGrtASM99tNnG1Q5A==;
In-Reply-To: <005a01da7c84$4b9ba5f0$e2d2f1d0$@gmail.com> (avi e. gross's
message of "Fri, 22 Mar 2024 14:11:00 -0400")
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
X-Original-Sender: loris.bennett@fu-berlin.de
X-Originating-IP: 130.133.144.203
X-ZEDAT-Hint: PO
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <874jcustc4.fsf@zedat.fu-berlin.de>
X-Mailman-Original-References: <87zfurgb6t.fsf@zedat.fu-berlin.de>
<uti7n1$2ef92$1@dont-email.me>
<87plvmr93v.fsf@zedat.fu-berlin.de>
<005a01da7c84$4b9ba5f0$e2d2f1d0$@gmail.com>
 by: Loris Bennett - Mon, 25 Mar 2024 06:45 UTC

<avi.e.gross@gmail.com> writes:

> Loris wrote:
>
> "Yes, I was mistakenly thinking that the popping the element would leave
> me with the dict minus the popped key-value pair. Seem like there is no
> such function."
>
> Others have tried to explain and pointed out you can del and then use the
> changed dict.
>
> But consider the odd concept of writing your own trivial function.
>
> def remaining(adict, anitem):
> _ = adict.pop(anitem)
> # alternatively duse del on dict and item
> return adict
>
>
>>>> remaining({"first": 1, "second": 2, "third": 3}, "second")
> {'first': 1, 'third': 3}
>
>
> Or do you want to be able to call it as in dict.remaining(key) by
> subclassing your own variant of dict and adding a similar method?

No, 'del' does indeed do what I wanted, although I have now decided I
want something else :-) Nevertheless it is good to know that 'del'
exists, so that I don't have to reinvent it.

Cheers,

Loris

--
This signature is currently under constuction.

Re: Popping key causes dict derived from object to revert to object

<87zfumre9b.fsf@zedat.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29734&group=comp.lang.python#29734

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.bennett@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Re: Popping key causes dict derived from object to revert to object
Date: Mon, 25 Mar 2024 07:56:16 +0100
Organization: FUB-IT (ex-ZEDAT), Freie Universität Berlin
Lines: 52
Message-ID: <87zfumre9b.fsf@zedat.fu-berlin.de>
References: <87zfurgb6t.fsf@zedat.fu-berlin.de> <uti7n1$2ef92$1@dont-email.me>
<87plvmr93v.fsf@zedat.fu-berlin.de> <4V1Rrq72BnznWJ6@mail.python.org>
<mailman.13.1711123277.3468.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de m3IRpmxPehRPBq9wvernTAJ6jGc9/VNEsowDK/OoWURfcU
Cancel-Lock: sha1:SCxLmN37qcnGwVS3juWaZjAAlIs= sha1:nHenJSxEq4doS0mCkKgQsen6nr4= sha256:QjryvJ0i34u9IN4JGP78QqAC6llobbYIGux20n0qwEg=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
 by: Loris Bennett - Mon, 25 Mar 2024 06:56 UTC

Grant Edwards <grant.b.edwards@gmail.com> writes:

> On 2024-03-22, Loris Bennett via Python-list <python-list@python.org> wrote:
>
>> Yes, I was mistakenly thinking that the popping the element would
>> leave me with the dict minus the popped key-value pair.
>
> It does.

Indeed, but I was thinking in the context of

dict_list = [d.pop('a') for d in dict_list]

and incorrectly expecting to get a list of 'd' without key 'a', instead
of a list of the 'd['a]'.

>> Seem like there is no such function.
>
> Yes, there is. You can do that with either pop or del:
>
> >>> d = {'a':1, 'b':2, 'c':3}
> >>> d
> {'a': 1, 'b': 2, 'c': 3}
> >>> d.pop('b')
> 2
> >>> d
> {'a': 1, 'c': 3}
>
>
> >>> d = {'a':1, 'b':2, 'c':3}
> >>> del d['b']
> >>> d
> {'a': 1, 'c': 3}
>
> In both cases, you're left with the dict minus the key/value pair.
>
> In the first case, the deleted value printed by the REPL because it
> was returned by the expression "d.pop('b')" (a method call).
>
> In the second case is no value shown by the REPL because "del d['b']"
> is a statement not an expression.

Thanks for pointing out 'del'. My main problem, however, was failing to
realise that the list comprehension is populated by the return value of
the 'pop', not the popped dict.

Cheers,

Loris

--
This signature is currently under constuction.

Re: Popping key causes dict derived from object to revert to object

<utrvj4$1440d$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29736&group=comp.lang.python#29736

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: michael.stemper@gmail.com (Michael F. Stemper)
Newsgroups: comp.lang.python
Subject: Re: Popping key causes dict derived from object to revert to object
Date: Mon, 25 Mar 2024 08:52:36 -0500
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <utrvj4$1440d$1@dont-email.me>
References: <87zfurgb6t.fsf@zedat.fu-berlin.de> <uti7n1$2ef92$1@dont-email.me>
<87plvmr93v.fsf@zedat.fu-berlin.de> <4V1Rrq72BnznWJ6@mail.python.org>
<mailman.13.1711123277.3468.python-list@python.org>
<87zfumre9b.fsf@zedat.fu-berlin.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 25 Mar 2024 14:52:37 +0100 (CET)
Injection-Info: dont-email.me; posting-host="86318455a6a084657f7e1558145f30ed";
logging-data="1183757"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/uKDmvcv3ulvBkk73xly8qi/7n5z+noSw="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:qlrF2DItX5jCohufGISjsoufxFI=
In-Reply-To: <87zfumre9b.fsf@zedat.fu-berlin.de>
Content-Language: en-US
 by: Michael F. Stemper - Mon, 25 Mar 2024 13:52 UTC

On 25/03/2024 01.56, Loris Bennett wrote:
> Grant Edwards <grant.b.edwards@gmail.com> writes:
>
>> On 2024-03-22, Loris Bennett via Python-list <python-list@python.org> wrote:
>>
>>> Yes, I was mistakenly thinking that the popping the element would
>>> leave me with the dict minus the popped key-value pair.
>>
>> It does.
>
> Indeed, but I was thinking in the context of
>
> dict_list = [d.pop('a') for d in dict_list]
>
> and incorrectly expecting to get a list of 'd' without key 'a', instead
> of a list of the 'd['a]'.
I apologize if this has already been mentioned in this thread, but are
you aware of "d.keys()" and "d.values"?

>>> d = {}
>>> d['do'] = 'a deer, a female deer'
>>> d['re'] = 'a drop of golden sunshine'
>>> d['mi'] = 'a name I call myself'
>>> d['fa'] = 'a long, long way to run'
>>> d.keys()
['fa', 'mi', 'do', 're']
>>> d.values()
['a long, long way to run', 'a name I call myself', 'a deer, a female deer', 'a drop of golden sunshine']
>>>

--
Michael F. Stemper
Exodus 22:21

Re: Popping key causes dict derived from object to revert to object

<87le66pdcw.fsf@zedat.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29737&group=comp.lang.python#29737

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.bennett@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Re: Popping key causes dict derived from object to revert to object
Date: Mon, 25 Mar 2024 15:58:39 +0100
Organization: FUB-IT (ex-ZEDAT), Freie Universität Berlin
Lines: 42
Message-ID: <87le66pdcw.fsf@zedat.fu-berlin.de>
References: <87zfurgb6t.fsf@zedat.fu-berlin.de> <uti7n1$2ef92$1@dont-email.me>
<87plvmr93v.fsf@zedat.fu-berlin.de> <4V1Rrq72BnznWJ6@mail.python.org>
<mailman.13.1711123277.3468.python-list@python.org>
<87zfumre9b.fsf@zedat.fu-berlin.de> <utrvj4$1440d$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de Tt5hmDbgJiZ2y6ucA4SNwwjxGbo1nAYNNaA0R6JYfouKQF
Cancel-Lock: sha1:jVFNko/7OuN2skybFJGRT6MN7TI= sha1:AYkfdKajBBOo5EE38Wrrarj5zYw= sha256:ZGWBMFviW1hpnbSoGYkNkmyXTrnzfhL+LQRxHHMyVZk=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
 by: Loris Bennett - Mon, 25 Mar 2024 14:58 UTC

"Michael F. Stemper" <michael.stemper@gmail.com> writes:

> On 25/03/2024 01.56, Loris Bennett wrote:
>> Grant Edwards <grant.b.edwards@gmail.com> writes:
>>
>>> On 2024-03-22, Loris Bennett via Python-list <python-list@python.org> wrote:
>>>
>>>> Yes, I was mistakenly thinking that the popping the element would
>>>> leave me with the dict minus the popped key-value pair.
>>>
>>> It does.
>> Indeed, but I was thinking in the context of
>> dict_list = [d.pop('a') for d in dict_list]
>> and incorrectly expecting to get a list of 'd' without key 'a',
>> instead
>> of a list of the 'd['a]'.
> I apologize if this has already been mentioned in this thread, but are
> you aware of "d.keys()" and "d.values"?
>
> >>> d = {}
> >>> d['do'] = 'a deer, a female deer'
> >>> d['re'] = 'a drop of golden sunshine'
> >>> d['mi'] = 'a name I call myself'
> >>> d['fa'] = 'a long, long way to run'
> >>> d.keys()
> ['fa', 'mi', 'do', 're']
> >>> d.values()
> ['a long, long way to run', 'a name I call myself', 'a deer, a female deer', 'a drop of golden sunshine']
> >>>

Yes, I am, thank you. However, I didn't want either the keys or the
values. Instead I wanted to remove a key within a list comprehension.

Cheers,

Loris

PS: "a drop of golden *sun*" - rhymes with "a long, long way to run"

--
This signature is currently under constuction.

Re: Popping key causes dict derived from object to revert to object

<slrnv035fl.81t.jon+usenet@raven.unequivocal.eu>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29738&group=comp.lang.python#29738

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jon+usenet@unequivocal.eu (Jon Ribbens)
Newsgroups: comp.lang.python
Subject: Re: Popping key causes dict derived from object to revert to object
Date: Mon, 25 Mar 2024 15:19:17 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <slrnv035fl.81t.jon+usenet@raven.unequivocal.eu>
References: <87zfurgb6t.fsf@zedat.fu-berlin.de>
<uti7n1$2ef92$1@dont-email.me> <87plvmr93v.fsf@zedat.fu-berlin.de>
<4V1Rrq72BnznWJ6@mail.python.org>
<mailman.13.1711123277.3468.python-list@python.org>
<87zfumre9b.fsf@zedat.fu-berlin.de> <utrvj4$1440d$1@dont-email.me>
<87le66pdcw.fsf@zedat.fu-berlin.de>
Injection-Date: Mon, 25 Mar 2024 16:19:17 +0100 (CET)
Injection-Info: dont-email.me; posting-host="e2acf20fbe44557349d898fcf40829a5";
logging-data="1230722"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18tIDv8kquvrGGljKQTo7nKDJm0FoDmLgQ="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:6tUK59W+1OyKo1wEwz4715wf0g4=
 by: Jon Ribbens - Mon, 25 Mar 2024 15:19 UTC

On 2024-03-25, Loris Bennett <loris.bennett@fu-berlin.de> wrote:
> "Michael F. Stemper" <michael.stemper@gmail.com> writes:
>
>> On 25/03/2024 01.56, Loris Bennett wrote:
>>> Grant Edwards <grant.b.edwards@gmail.com> writes:
>>>
>>>> On 2024-03-22, Loris Bennett via Python-list <python-list@python.org> wrote:
>>>>
>>>>> Yes, I was mistakenly thinking that the popping the element would
>>>>> leave me with the dict minus the popped key-value pair.
>>>>
>>>> It does.
>>> Indeed, but I was thinking in the context of
>>> dict_list = [d.pop('a') for d in dict_list]
>>> and incorrectly expecting to get a list of 'd' without key 'a',
>>> instead
>>> of a list of the 'd['a]'.
>> I apologize if this has already been mentioned in this thread, but are
>> you aware of "d.keys()" and "d.values"?
>>
>> >>> d = {}
>> >>> d['do'] = 'a deer, a female deer'
>> >>> d['re'] = 'a drop of golden sunshine'
>> >>> d['mi'] = 'a name I call myself'
>> >>> d['fa'] = 'a long, long way to run'
>> >>> d.keys()
>> ['fa', 'mi', 'do', 're']
>> >>> d.values()
>> ['a long, long way to run', 'a name I call myself', 'a deer, a female deer', 'a drop of golden sunshine']
>> >>>
>
> Yes, I am, thank you. However, I didn't want either the keys or the
> values. Instead I wanted to remove a key within a list comprehension.

Do you mean something like:

[my_dict[key] for key in my_dict if key != 'a']

?

Re: Popping key causes dict derived from object to revert to object

<mailman.25.1711382101.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29739&group=comp.lang.python#29739

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!newsfeed.endofthelinebbs.com!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: grant.b.edwards@gmail.com (Grant Edwards)
Newsgroups: comp.lang.python
Subject: Re: Popping key causes dict derived from object to revert to object
Date: Mon, 25 Mar 2024 11:54:59 -0400 (EDT)
Lines: 31
Message-ID: <mailman.25.1711382101.3468.python-list@python.org>
References: <87zfurgb6t.fsf@zedat.fu-berlin.de>
<uti7n1$2ef92$1@dont-email.me> <87plvmr93v.fsf@zedat.fu-berlin.de>
<4V1Rrq72BnznWJ6@mail.python.org>
<mailman.13.1711123277.3468.python-list@python.org>
<87zfumre9b.fsf@zedat.fu-berlin.de>
<4V3HZC52gbznVFV@mail.python.org>
X-Trace: news.uni-berlin.de t8Ktx3VeIwmonaQkBBM8RAs7ERWngHP6AwC4HBwfNU4w==
Cancel-Lock: sha1:TCVY7KblJ4Vs6uYv57y0potr/Bs= sha256:6EEDm/V8v72PID0WHxeEihhb3uL8Pg9QdLIEyzLZpQY=
Return-Path: <grant.b.edwards@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.030
X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'minus': 0.09; 'ok,': 0.09;
'writes:': 0.09; '"leave': 0.16; 'bennett': 0.16; 'dict': 0.16;
'from:addr:grant.b.edwards': 0.16; 'from:name:grant edwards':
0.16; 'key-value': 0.16; 'suffice.': 0.16; 'wrote:': 0.16;
'grant': 0.17; 'instead': 0.17; 'to:addr:python-list': 0.20;
'problem,': 0.22; 'returns': 0.22; 'object': 0.26; 'leave': 0.27;
'function': 0.27; '>>>': 0.28; 'thinking': 0.28; 'header:User-
Agent:1': 0.30; 'whole': 0.30; 'context': 0.32; 'objects': 0.32;
'python-list': 0.32; "wouldn't": 0.32; 'but': 0.32; 'same': 0.34;
'mean': 0.34; 'yes,': 0.35; 'from:addr:gmail.com': 0.35; 'main':
0.37; 'subject:from': 0.37; 'thanks': 0.38; 'otherwise': 0.39;
'list': 0.39; 'method': 0.61; "there's": 0.61; 'between': 0.63;
'key': 0.64; 'message-id:invalid': 0.68; 'populated': 0.69;
'pointing': 0.84; 'realise': 0.84; 'pop': 0.91
User-Agent: slrn/1.0.3 (Linux)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <4V3HZC52gbznVFV@mail.python.org>
X-Mailman-Original-References: <87zfurgb6t.fsf@zedat.fu-berlin.de>
<uti7n1$2ef92$1@dont-email.me> <87plvmr93v.fsf@zedat.fu-berlin.de>
<4V1Rrq72BnznWJ6@mail.python.org>
<mailman.13.1711123277.3468.python-list@python.org>
<87zfumre9b.fsf@zedat.fu-berlin.de>
 by: Grant Edwards - Mon, 25 Mar 2024 15:54 UTC

On 2024-03-25, Loris Bennett via Python-list <python-list@python.org> wrote:
> Grant Edwards <grant.b.edwards@gmail.com> writes:
>
>> On 2024-03-22, Loris Bennett via Python-list <python-list@python.org> wrote:
>>
>>> Yes, I was mistakenly thinking that the popping the element would
>>> leave me with the dict minus the popped key-value pair.
>>
>> It does.
>
> Indeed, but I was thinking in the context of
>
> dict_list = [d.pop('a') for d in dict_list]
>
> and incorrectly expecting to get a list of 'd' without key 'a', instead
> of a list of the 'd['a]'.

So when you say "leave me with", you mean "return the same dictionary
with"? There's an important difference between what a function
returns and what global/local state it "leaves you with".

> Thanks for pointing out 'del'. My main problem, however, was
> failing to realise that the list comprehension is populated by the
> return value of the 'pop', not the popped dict.

OK, so perhaps you weren't execting the original dict objects to be
mutated, but rather that the pop method would return a new dict object
without the "popped" element. The whole point of the 'pop method is to
return the popped value, otherwise it wouldn't be needed. The 'del'
statement would suffice.

RE: Popping key causes dict derived from object to revert to object

<mailman.26.1711382694.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29740&group=comp.lang.python#29740

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: <avi.e.gross@gmail.com>
Newsgroups: comp.lang.python
Subject: RE: Popping key causes dict derived from object to revert to object
Date: Mon, 25 Mar 2024 12:04:48 -0400
Lines: 109
Message-ID: <mailman.26.1711382694.3468.python-list@python.org>
References: <87zfurgb6t.fsf@zedat.fu-berlin.de>
<uti7n1$2ef92$1@dont-email.me> <87plvmr93v.fsf@zedat.fu-berlin.de> <005a01da7c84$4b9ba5f0$e2d2f1d0$@gmail.com>
<874jcustc4.fsf@zedat.fu-berlin.de>
<004901da7ece$29b3e870$7d1bb950$@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de bWXgUEo/RS5JygbiwlmwkwudB8OMxH1yaPP/0ugueJyQ==
Cancel-Lock: sha1:8ZpdMRx7ADe7iR1vEsUB+aOzwVE= sha256:8OcPJnH6rGZrhios2KkeAQAdWFb4NhGtfKK+umORH+o=
Return-Path: <avi.e.gross@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=aYKe1MLC;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.009
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'def': 0.04; 'fairly':
0.05; 'indicate': 0.05; 'that?': 0.07; ':-)': 0.09; 'cc:addr
:python-list': 0.09; 'derived': 0.09; 'ended': 0.09; 'minus':
0.09; 'received:108': 0.09; 'trivial': 0.09; 'writes:': 0.09;
'cheers,': 0.11; 'cc:no real name:2**0': 0.14; '2024': 0.16;
'>>>>': 0.16; 'bennett': 0.16; 'choices.': 0.16; 'collection.':
0.16; 'column': 0.16; 'columns': 0.16; 'dict': 0.16;
'dictionary,': 0.16; 'dicts': 0.16; 'idiom': 0.16; 'ignored':
0.16; 'indeed': 0.16; 'intact': 0.16; 'iterate': 0.16; 'key-
value': 0.16; 'method?': 0.16; 'nested': 0.16; 'odd': 0.16;
'sets.': 0.16; 'skip:. 20': 0.16; 'structures': 0.16; 'variant':
0.16; 'ways.': 0.16; 'wrote:': 0.16; 'python': 0.16; 'values':
0.17; 'instead': 0.17; 'message-id:@gmail.com': 0.18;
'cc:addr:python.org': 0.20; 'language': 0.21; 'languages': 0.22;
'doubt': 0.22; 'way.': 0.22; 'skip:- 10': 0.25; 'cc:2**0': 0.25;
'tried': 0.26; 'object': 0.26; 'suspect': 0.26; 'leave': 0.27;
'else': 0.27; 'bit': 0.27; 'done': 0.28; 'thinking': 0.28;
'keeping': 0.28; 'wrong': 0.28; 'seem': 0.31; 'modify': 0.31;
'think': 0.32; 'assume': 0.32; 'concept': 0.32; 'do.': 0.32;
'extract': 0.32; 'modified': 0.32; 'returning': 0.32; 'but': 0.32;
'there': 0.33; 'march': 0.33; 'able': 0.34; 'header:In-Reply-
To:1': 0.34; 'received:google.com': 0.34; 'complex': 0.35; 'felt':
0.35; 'item': 0.35; 'from:addr:gmail.com': 0.35; 'functions':
0.36; 'people': 0.36; 'lists': 0.37; 'currently': 0.37;
'subject:from': 0.37; 'using': 0.37; 'others': 0.37; 'could':
0.38; 'two': 0.39; 'adding': 0.39; 'single': 0.39; 'enough': 0.39;
'use': 0.39; 'methods': 0.39; 'rest': 0.39; 'still': 0.40;
'explain': 0.40; 'processed': 0.40; 'something': 0.40; 'want':
0.40; 'including': 0.60; 'method': 0.61; 'from:': 0.62; 'to:':
0.62; '25,': 0.62; 'here': 0.62; 'ever': 0.63; 'email
addr:gmail.com': 0.63; 'simply': 0.63; 'copy': 0.63; 'everything':
0.63; 'hear': 0.64; 'key': 0.64; 're:': 0.64; 'remains': 0.64;
'thus': 0.64; 'your': 0.64; 'supply': 0.65; 'similar': 0.65;
'well': 0.65; 'decided': 0.67; 'guaranteed': 0.67; 'right': 0.68;
'items': 0.68; 'revert': 0.68; 'order': 0.69; 'cc:': 0.69;
'order.': 0.69; 'remaining': 0.69; 'del': 0.70; 'costs': 0.74;
'signature': 0.76; 'sent:': 0.78; 'major': 0.78; 'out.': 0.80;
'left': 0.83; 'practical': 0.84; 'became': 0.84; 'exclusion':
0.84; 'ordered': 0.84; 'pipeline': 0.84; 'received:mail-
qk1-x735.google.com': 0.84; 'opposite': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1711382691; x=1711987491; darn=python.org;
h=thread-index:content-language:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:cc:to
:from:from:to:cc:subject:date:message-id:reply-to;
bh=F0SjZ+AXaPwUj4uQt5zkQ3mvCYLbVsVoMZAYSLWFWhk=;
b=aYKe1MLCmG/BBQLDNK+qMH3CYgglu60Qm1lFeIrYDgHGvwZrz2YncCe5iM4sxjUnJ6
MgN1jQ36+ZQXXZ54DH8KcWyw97SwP7i/s8nAsZApaHypOPVuIfjR9StIPEPDMFjZS3XF
wlDIXC3ZFgqDyQ+9qM/4Gb7BL875M/A1wUtcfMC0+cx/etaxb/DVQIAA4LU8aHEHfEfl
iUB8mMY4e/j6iw2EDw9t82Oh8RTK9UsYUyiREw+2nO0lsUBs6/O4SdA7r/GQIsKzWdf+
rvGkIXAAOgl4/TFoFJA4pKH0ZNoklYDbH8AMZTtvNhqMlOpHWpkTqzeRCc3qTYRC5beL
fXkw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1711382691; x=1711987491;
h=thread-index:content-language:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:cc:to
:from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=F0SjZ+AXaPwUj4uQt5zkQ3mvCYLbVsVoMZAYSLWFWhk=;
b=uQKXh82DO56XzokGyhqrhU8LF+rTloQLPDy2TGoO8tNz559T9AbIVYA9GIbI9QzhcD
yPcuQzBRX2VFUTqygIKbnm3TOimzFGpdCLu244AqbnIsO7UlBJ/4M7xRJ63TAvQvvw4t
ehVvKnui9x6mOwO+uew/4njoRUqh8PP35UQDA5v+MayylwpLfVuF3C17fYUGZ8HKTIMh
StsYfc+ONDXXOt2Da/ZgJb8qFQuBTSG4z1IElCTQjkR1k/+3IIOm7Y+UacpbLj0t1j4B
rDfnKvZqNWzFsSDRGtd7A1kpe2UM+bj3HADTrR/AM0wiSUORheUL69NyyGSkw24qNlSm
UfAQ==
X-Gm-Message-State: AOJu0Yw4gamM0uK4sPf2kFkQ54snQH1/KW3gCZA2bVeO7/xxMFceGYRB
uB046o1B/16G31fTeoI+ZJZrJtus0JxF7q2K2tmbhjVGTcCE2epm
X-Google-Smtp-Source: AGHT+IHTLGS+O8UXxSvA57gX2+oTVsswuptkxLwgsi/2sBRcrWfqohlgLLZof2JTdtJWGNqqBV2SUA==
X-Received: by 2002:a05:620a:1279:b0:78a:4338:7956 with SMTP id
b25-20020a05620a127900b0078a43387956mr7700793qkl.34.1711382690899;
Mon, 25 Mar 2024 09:04:50 -0700 (PDT)
In-Reply-To: <874jcustc4.fsf@zedat.fu-berlin.de>
X-Mailer: Microsoft Outlook 16.0
Content-Language: en-us
Thread-Index: AQKM7iLteXOq+KoZ2KtNA4Zjj8pI1wKmK1mHAVVm8JoB61U1HAHbvCMVr6WflpA=
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <004901da7ece$29b3e870$7d1bb950$@gmail.com>
X-Mailman-Original-References: <87zfurgb6t.fsf@zedat.fu-berlin.de>
<uti7n1$2ef92$1@dont-email.me> <87plvmr93v.fsf@zedat.fu-berlin.de> <005a01da7c84$4b9ba5f0$e2d2f1d0$@gmail.com>
<874jcustc4.fsf@zedat.fu-berlin.de>
 by: <avi.e.gross@gmail.com> - Mon, 25 Mar 2024 16:04 UTC

I am glad, Lori, you found a solution another way.

Actually, Lori, I think you were right in looking for a built-in method that complements pop() by returning everything else other than the item mentioned. There are philosophical and practical considerations that were no doubt considered and a reality that the functionality did exist albeit not in a pipelined format.

Consider a language like LISP which sort of processed lists of things in which a major concept was getting the first item or getting all the rest. Lots of LISP programs had complex structures using CAR() and CDR() nested various ways to say extract the third item as CAR(CDR(CDR(X))) to the point where some commonly used combos became functions with names like CADDR().

There are again many languages where functions or methods are available that include an exclusion variant from a collection perhaps by doing something as simple as using a minus sign to indicate what to remove, as is commonly done in R to remove a column in a data.frame while keeping the order of the remaining columns the same.

Lots of languages had similar concepts about ordered data structures but dictionaries may be a bit of something else and initially in Python were not guaranteed to have any kind of order. Python dicts are more like unordered sets.

So although there remains a concept of not first/rest but this/rest, I suspect there was some thought about the process that ended in deciding not to supply some functionality. When you use pop() on something like a large dictionary, the original is left intact and is ignored and a copy of a single element is made and returned. To do the opposite and return the rest has two choices. One is to make a big copy of the rest of the dictionary and the other is to use del internally and return the modified dict. The functions you could write do the latter.

So why not add one or more methods to do that? Who knows? But I think some may have considered it not needed including some who felt no need for a pipeline method when del would do. Another consideration was the common idiom for iterating on a collection. Besides pop() you can get lists of dictionary entries, keys or values that you can work with and you can even iterate with "for key in dict: ..."

Given how many ways common things can be done, and given that adding too many methods has costs including new users not understanding all the nuanced differences, this fairly useful functionality was left out.

Unfortunately, I think they were wrong here as instead we hear often from people like you who assume things would work other ways. I still think it would be simple enough to have had a .removekeys(keys) that would work in a pipeline to modify the dict by removing one or more items and perhaps another .removevalues(values) but at some point you may keep adding methods nobody ever uses. The reality is that many trivial one-liner comprehensions can easily do many such things using iteration.

But many simply do not work well in pipelined fashion and thus may need to be embedded in a method of your own by subclassing dict or rolling your own.

-----Original Message-----
From: Loris Bennett <loris.bennett@fu-berlin.de>
Sent: Monday, March 25, 2024 2:45 AM
To: avi.e.gross@gmail.com
Cc: python-list@python.org
Subject: Re: Popping key causes dict derived from object to revert to object

<avi.e.gross@gmail.com> writes:

> Loris wrote:
>
> "Yes, I was mistakenly thinking that the popping the element would leave
> me with the dict minus the popped key-value pair. Seem like there is no
> such function."
>
> Others have tried to explain and pointed out you can del and then use the
> changed dict.
>
> But consider the odd concept of writing your own trivial function.
>
> def remaining(adict, anitem):
> _ = adict.pop(anitem)
> # alternatively duse del on dict and item
> return adict
>
>
>>>> remaining({"first": 1, "second": 2, "third": 3}, "second")
> {'first': 1, 'third': 3}
>
>
> Or do you want to be able to call it as in dict.remaining(key) by
> subclassing your own variant of dict and adding a similar method?

No, 'del' does indeed do what I wanted, although I have now decided I
want something else :-) Nevertheless it is good to know that 'del'
exists, so that I don't have to reinvent it.

Cheers,

Loris

--
This signature is currently under constuction.

RE: Popping key causes dict derived from object to revert to object

<mailman.27.1711383557.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29741&group=comp.lang.python#29741

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: <avi.e.gross@gmail.com>
Newsgroups: comp.lang.python
Subject: RE: Popping key causes dict derived from object to revert to object
Date: Mon, 25 Mar 2024 12:19:12 -0400
Lines: 84
Message-ID: <mailman.27.1711383557.3468.python-list@python.org>
References: <87zfurgb6t.fsf@zedat.fu-berlin.de> <uti7n1$2ef92$1@dont-email.me>
<87plvmr93v.fsf@zedat.fu-berlin.de> <4V1Rrq72BnznWJ6@mail.python.org>
<mailman.13.1711123277.3468.python-list@python.org>
<87zfumre9b.fsf@zedat.fu-berlin.de>
<006001da7ed0$2cb3f720$861be560$@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de QKfg9vot/WFecs6Hdcdj/gBl4PetjbYl/KAL1/XuSUMg==
Cancel-Lock: sha1:60qAIjjtE//yn8tybnU74tzTmcs= sha256:pfhFdk08UwF5iUO1SlSN1TZaVvSuauUPFX2AOB4Ogu0=
Return-Path: <avi.e.gross@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=Incuj8aZ;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.008
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'is.': 0.05; 'derived':
0.09; 'expression': 0.09; 'minus': 0.09; 'received:108': 0.09;
'received:mail-qk1-x72b.google.com': 0.09; 'values.': 0.09;
'writes:': 0.09; 'cheers,': 0.11; 'url:mailman': 0.15; 'that.':
0.15; '2024': 0.16; 'bennett': 0.16; 'cases,': 0.16; 'comparing':
0.16; 'dict': 0.16; 'dictionary,': 0.16; 'dictionary.': 0.16;
'hash': 0.16; 'idiom': 0.16; 'key-value': 0.16; 'linear': 0.16;
'specify': 0.16; 'wrote:': 0.16; 'grant': 0.17; 'instead': 0.17;
'message-id:@gmail.com': 0.18; 'to:addr:python-list': 0.20;
'bit.': 0.22; 'problem,': 0.22; 'code': 0.23; 'list,': 0.24;
'idea': 0.24; 'skip:- 10': 0.25; 'url-ip:188.166.95.178/32': 0.25;
'url-ip:188.166.95/24': 0.25; 'url:listinfo': 0.25; 'url-
ip:188.166/16': 0.25; 'object': 0.26; 'leave': 0.27; 'done': 0.28;
'>>>': 0.28; 'purpose': 0.28; 'thinking': 0.28; 'printed': 0.28;
'seem': 0.31; 'url-ip:188/8': 0.31; 'context': 0.32; 'deleted':
0.32; 'modified': 0.32; 'python-list': 0.32; 'but': 0.32; 'there':
0.33; 'march': 0.33; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'item': 0.35; 'yes,': 0.35;
'from:addr:gmail.com': 0.35; 'change': 0.36; 'currently': 0.37;
'main': 0.37; 'subject:from': 0.37; 'using': 0.37; 'could': 0.38;
'means': 0.38; 'thanks': 0.38; 'list': 0.39; 'both': 0.40;
'something': 0.40; 'want': 0.40; 'method': 0.61; 'from:': 0.62;
'to:': 0.62; '25,': 0.62; 'great': 0.63; 'key': 0.64; 're:': 0.64;
'your': 0.64; 'time.': 0.66; 'items': 0.68; 'revert': 0.68;
'populated': 0.69; 'del': 0.70; 'compared': 0.71; 'signature':
0.76; 'sent:': 0.78; 'returned': 0.81; 'left': 0.83; 'clause':
0.84; 'pointing': 0.84; 'realise': 0.84; 'pop': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1711383555; x=1711988355; darn=python.org;
h=thread-index:content-language:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:to:from
:from:to:cc:subject:date:message-id:reply-to;
bh=3+jN0GHSlnsyaq4SrJePYu5M2lqnL5g91xLZ3OD2TiA=;
b=Incuj8aZH84yJADHSBOgwUZMqu5jsPunndrycm6SQ6I14p9op6/I2IZpRnlyR7tB2N
ChfFfZvr1Z7DG/+yFupG1PHJbPFJNleFtI8QDOHxWS6E+PgAdQtM+sW/6ezlNY/Vkl9x
WzHRkfbD1RCaw5S5XZ2fkn51S+fVinv+hpM3vt8b6/Mvj87aAZLNDCGzYSgBBg6gbCdy
kdlmgWu7uyfkw4TCXl8t7fVC+tfUg8hfZtoe0e+WYrLQ0LH3oHl4/ECmihJKcfwF5p0g
pvhpkwX16WpUffSd+NZrU1SmygUFgWml664Gqkqo1CpMZBxt4IDTaUgN7MtDUR5fHE3j
38hw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1711383555; x=1711988355;
h=thread-index:content-language:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:to:from
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=3+jN0GHSlnsyaq4SrJePYu5M2lqnL5g91xLZ3OD2TiA=;
b=P71d9ixgf7I4Lj07dgDeaGg/6vMLw0V2nasTV6IJD9qSofs/DdnxPYD1iJe/7bGzm7
9BnMFPKo3X6ovUDzVhVnZjYDQ4Mr7F0NlvRiFROH9bicKuDXR0y3DHvTqychd9FN3YfE
vwXKtwPQk4gqr71HCC3w5iL+5WbvNYU0Th+7A42NXJMcsodQ/tplRKtJ4yqrLZq/oFI5
jdEqcojlQ+bmPBsk+1qHGbQqi/ccGSRh5meu/Ro+RbMeEL6xo1Tr9TFoWwgU8QnBnuSL
+znYBkiUIGGWiQK0uGJ0aYGV3GBCE1fW6j639eWfFtV3v/xYOXHJOArVsc9Josyudakp
yo9Q==
X-Forwarded-Encrypted: i=1;
AJvYcCXjw6mAiuTVNmH70WqVvxhr8ymMEWaKJsMNue6/vfqdljRYhlB5XaIn9jafS9o8riHEzj9O33cgPBvnVZBlsF1L3q2K91zQ
X-Gm-Message-State: AOJu0Yz8SBIblNxhdtFnEB96O66NGqI6b1+UUfPfkyxArxLYDn36K8lZ
Qn68Z5yGMqtMfLiQRwmgZr/bHwj8ZBW5yx5+CmUOiFPj9MRT4oXcvTH3RMPG
X-Google-Smtp-Source: AGHT+IGW2Oh4yQO0HkJnwTAcO9V+8k1rOL5Po0nore1hl1rcra4bcTiOi4XZNXpMtODQJbRoyuEtjQ==
X-Received: by 2002:a05:620a:2950:b0:78a:5f75:9e78 with SMTP id
n16-20020a05620a295000b0078a5f759e78mr481824qkp.5.1711383554898;
Mon, 25 Mar 2024 09:19:14 -0700 (PDT)
In-Reply-To: <87zfumre9b.fsf@zedat.fu-berlin.de>
X-Mailer: Microsoft Outlook 16.0
Content-Language: en-us
Thread-Index: AQKM7iLteXOq+KoZ2KtNA4Zjj8pI1wKmK1mHAVVm8JoCJiLdXQHK8c5CAbvP70Cvln6r4A==
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <006001da7ed0$2cb3f720$861be560$@gmail.com>
X-Mailman-Original-References: <87zfurgb6t.fsf@zedat.fu-berlin.de>
<uti7n1$2ef92$1@dont-email.me> <87plvmr93v.fsf@zedat.fu-berlin.de>
<4V1Rrq72BnznWJ6@mail.python.org>
<mailman.13.1711123277.3468.python-list@python.org>
<87zfumre9b.fsf@zedat.fu-berlin.de>
 by: <avi.e.gross@gmail.com> - Mon, 25 Mar 2024 16:19 UTC

Lori,

The list comprehension you are thinking of does work if you change things a
bit. But it is not a great idea as a main purpose of a dict is that using a
hash means things are found in linear time. A comprehension iterates on all
values. If you wanted to select just some items to keep in a list, your code
could be modified from:

dict_list = [d.pop('a') for d in dict_list]

to have an IF clause that would specify something like comparing it to the
item you do not want to keep.

But your idiom might be better done to make another dictionaly, not list
with something like:

New_dict = {key:value for key in dict if key != "whatever"}

Or variants on that. It builds a new dictionary, at nontrivial expense, as
compared to using del on an existing dictionary.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Loris Bennett via Python-list
Sent: Monday, March 25, 2024 2:56 AM
To: python-list@python.org
Subject: Re: Popping key causes dict derived from object to revert to object

Grant Edwards <grant.b.edwards@gmail.com> writes:

> On 2024-03-22, Loris Bennett via Python-list <python-list@python.org>
wrote:
>
>> Yes, I was mistakenly thinking that the popping the element would
>> leave me with the dict minus the popped key-value pair.
>
> It does.

Indeed, but I was thinking in the context of

dict_list = [d.pop('a') for d in dict_list]

and incorrectly expecting to get a list of 'd' without key 'a', instead
of a list of the 'd['a]'.

>> Seem like there is no such function.
>
> Yes, there is. You can do that with either pop or del:
>
> >>> d = {'a':1, 'b':2, 'c':3}
> >>> d
> {'a': 1, 'b': 2, 'c': 3}
> >>> d.pop('b')
> 2
> >>> d
> {'a': 1, 'c': 3}
>
>
> >>> d = {'a':1, 'b':2, 'c':3}
> >>> del d['b']
> >>> d
> {'a': 1, 'c': 3}
>
> In both cases, you're left with the dict minus the key/value pair.
>
> In the first case, the deleted value printed by the REPL because it
> was returned by the expression "d.pop('b')" (a method call).
>
> In the second case is no value shown by the REPL because "del d['b']"
> is a statement not an expression.

Thanks for pointing out 'del'. My main problem, however, was failing to
realise that the list comprehension is populated by the return value of
the 'pop', not the popped dict.

Cheers,

Loris

--
This signature is currently under constuction.
--
https://mail.python.org/mailman/listinfo/python-list

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor