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

Liste des GroupesRevenir à cl python 
Sujet : Re: Popping key causes dict derived from object to revert to object
De : loris.bennett (at) *nospam* fu-berlin.de (Loris Bennett)
Groupes : comp.lang.python
Date : 25. Mar 2024, 07:45:15
Autres entêtes
Organisation : FUB-IT (ex-ZEDAT), Freie Universität Berlin
Message-ID : <mailman.23.1711349357.3468.python-list@python.org>
References : 1 2 3 4 5
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
<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.

Date Sujet#  Auteur
21 Mar 24 * Popping key causes dict derived from object to revert to object14Loris Bennett
21 Mar 24 +- Re: Popping key causes dict derived from object to revert to object1dieter.maurer
21 Mar 24 `* Re: Popping key causes dict derived from object to revert to object12Mark Bourne
22 Mar 24  `* Re: Popping key causes dict derived from object to revert to object11Loris Bennett
22 Mar 24   +* Re: Popping key causes dict derived from object to revert to object7Grant Edwards
25 Mar 24   i`* Re: Popping key causes dict derived from object to revert to object6Loris Bennett
25 Mar 24   i +* Re: Popping key causes dict derived from object to revert to object3Michael F. Stemper
25 Mar 24   i i`* Re: Popping key causes dict derived from object to revert to object2Loris Bennett
25 Mar 24   i i `- Re: Popping key causes dict derived from object to revert to object1Jon Ribbens
25 Mar 24   i +- Re: Popping key causes dict derived from object to revert to object1Grant Edwards
25 Mar 24   i `- Re: Popping key causes dict derived from object to revert to object1<avi.e.gross
22 Mar 24   +- Re: Popping key causes dict derived from object to revert to object1<avi.e.gross
25 Mar 24   +- Re: Popping key causes dict derived from object to revert to object1Loris Bennett
25 Mar 24   `- Re: Popping key causes dict derived from object to revert to object1<avi.e.gross

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal