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 : <avi.e.gross (at) *nospam* gmail.com>
Groupes : comp.lang.python
Date : 25. Mar 2024, 17:19:12
Autres entêtes
Message-ID : <mailman.27.1711383557.3468.python-list@python.org>
References : 1 2 3 4 5 6 7
User-Agent : Microsoft Outlook 16.0
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


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