Deutsch English Français Italiano |
<slrnv035fl.81t.jon+usenet@raven.unequivocal.eu> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Jon Ribbens <jon+usenet@unequivocal.eu> Newsgroups: comp.lang.python Subject: Re: Popping key causes dict derived from object to revert to object Date: Mon, 25 Mar 2024 15:19:17 -0000 (UTC) Organization: A noiseless patient Spider Lines: 39 Message-ID: <slrnv035fl.81t.jon+usenet@raven.unequivocal.eu> 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> <utrvj4$1440d$1@dont-email.me> <87le66pdcw.fsf@zedat.fu-berlin.de> Injection-Date: Mon, 25 Mar 2024 16:19:17 +0100 (CET) Injection-Info: dont-email.me; posting-host="e2acf20fbe44557349d898fcf40829a5"; logging-data="1230722"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18tIDv8kquvrGGljKQTo7nKDJm0FoDmLgQ=" User-Agent: slrn/1.0.3 (Linux) Cancel-Lock: sha1:6tUK59W+1OyKo1wEwz4715wf0g4= Bytes: 2580 On 2024-03-25, Loris Bennett <loris.bennett@fu-berlin.de> wrote: > "Michael F. Stemper" <michael.stemper@gmail.com> writes: > >> On 25/03/2024 01.56, Loris Bennett 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]'. >> I apologize if this has already been mentioned in this thread, but are >> you aware of "d.keys()" and "d.values"? >> >> >>> d = {} >> >>> d['do'] = 'a deer, a female deer' >> >>> d['re'] = 'a drop of golden sunshine' >> >>> d['mi'] = 'a name I call myself' >> >>> d['fa'] = 'a long, long way to run' >> >>> d.keys() >> ['fa', 'mi', 'do', 're'] >> >>> d.values() >> ['a long, long way to run', 'a name I call myself', 'a deer, a female deer', 'a drop of golden sunshine'] >> >>> > > Yes, I am, thank you. However, I didn't want either the keys or the > values. Instead I wanted to remove a key within a list comprehension. Do you mean something like: [my_dict[key] for key in my_dict if key != 'a'] ?