Deutsch English Français Italiano |
<mailman.27.1711383557.3468.python-list@python.org> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!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> Bytes: 8718 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