Re: [Python-ideas] Add a method to get the subset of a dictionnary.
Oh, I forgot to mention... On Wed, Oct 12, 2016 at 06:06:51PM +0200, Enguerrand Pelletier wrote: > Hi all, > > It always bothered me to write something like this when i want to strip > keys from a dictionnary in Python: [snip] You've started a completely new discussion on an unrelated topic, but you appear to have done so by replying to an existing email conversation and editing the subject line. Specifically the thread with subject: "Improve error message when missing 'self' ..." Editing the subject line to indicate a change in topic is good! Thank you for doing that! But you should be aware that many email programs will thread this new, unrelated conversation under the "Improve error message" conversation. That means some people may see a threaded view something like this: Subject: [Python-ideas] Improve error message when missing 'self' ... ├─> Re: [Python-ideas] Improve error message ... │ └─> │ └─┬─> │ │ └─> │ └─> └─>[Python-ideas] Add a method to get the subset ... └─> Re: [Python-ideas] Add a method to get ... which may not matter if they are actually related threads, but anyone who has blocked or muted the original "Improve error message..." will not see your new conversation either. This may be considered part of the older conversation, and muted as well. When you plan to start a brand new conversation, unrelated to an existing thread, it is best to start a fresh email, don't reply to an existing one. Changing the subject line is not sufficient to break the link between your email and the previous conversation. You should use your email program's New Email command, type in the new subject line, and then set the To address to python-ideas@python.org. That's the only way to avoid the risk that some people won't see your email because they've muted the parent thread. Hope this helps, Steve ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Add a method to get the subset of a dictionnary.
On 10/12/2016 5:52 PM, Terry Reedy wrote: On 10/12/2016 12:06 PM, Enguerrand Pelletier wrote: b = {k, v for k,v in a.items() if k in interesting_keys} Test code before posting. The above is a set comprehension creating a set of tupes. I should have followed my own advice. The above is a SyntaxError until 'k,v' is wrapped in parens, '(k,v)'. -- Terry Jan Reedy ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Add a method to get the subset of a dictionnary.
On 10/12/2016 12:06 PM, Enguerrand Pelletier wrote: Hi all, It always bothered me to write something like this when i want to strip keys from a dictionnary in Python: a = {"foo": 1, "bar": 2, "baz": 3, "foobar": 42} interesting_keys = ["foo", "bar", "baz"] If the keys are hashable, this should be a set. b = {k, v for k,v in a.items() if k in interesting_keys} Test code before posting. The above is a set comprehension creating a set of tupes. For a dict, 'k, v' must be 'k:v'. Wouldn't it be nice to have a syntactic sugar such as: b = a.subset(interesting_keys) It is pretty rare for the filter condition to be exactly 'key in explicit_keys'. If it is, one can directly construct the dict from a and explict_keys. b = {k:a[k] for k in interesting_keys} The syntactic sugar wrapping this would save 6 keypresses. Interesting_keys can be any iterable. To guarantee no KeyErrors, add 'if k in a'. -- Terry Jan Reedy ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Add a method to get the subset of a dictionnary.
That discussion seemed to mostly just conclude that dicts shouldn't have all set operations, and then it kind of just dropped off. No one really argued the subset part. -- Ryan [ERROR]: Your autotools build scripts are 200 lines longer than your program. Something’s wrong. http://kirbyfan64.github.io/ On Oct 12, 2016 11:33 AM, "Riley Banks"wrote: > Looks like it was discussed before: > https://mail.python.org/pipermail/python-ideas/2012-January/013252.html > ___ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ > ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/