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 : grant.b.edwards (at) *nospam* gmail.com (Grant Edwards)
Groupes : comp.lang.python
Date : 25. Mar 2024, 16:54:59
Autres entêtes
Message-ID : <mailman.25.1711382101.3468.python-list@python.org>
References : 1 2 3 4 5 6 7
User-Agent : slrn/1.0.3 (Linux)
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.


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