Deutsch English Français Italiano |
<87zfumre9b.fsf@zedat.fu-berlin.de> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.mixmin.net!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: "Loris Bennett" <loris.bennett@fu-berlin.de> 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 =?utf-8?Q?Universit=C3=A4t?= 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) Bytes: 2403 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.