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 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: > > a = {"foo": 1, "bar": 2, "baz": 3, "foobar": 42} > interesting_keys = ["foo", "bar", "baz"] > b = {k, v for k,v in a.items() if k in interesting_keys} (You have a small typo: should be "k: v" not "k, v".) Why does it bother you? It is simple, easy to understand, and explict. > Wouldn't it be nice to have a syntactic sugar such as: Syntactic sugar is not really the right term, "syntactic sugar" means a special form of syntax as a short-cut for something longer. This is not special syntax, it is just a method. > b = a.subset(interesting_keys) Personally, I have never missed this method, but if I did, it would be easy to make a helper function: def subset(adict, keys): """Return a new dict from existing keys.""" return {k: v for k,v in a.items() if k in keys} Not every simple helper function needs to be built-in to the class. That is the beauty of Python, you can make your own helper functions, once you give up the idea that everything needs to be a method. There are some problems with making this a method. To start with, it means that every dict and mapping would have to support it. Perhaps that is acceptible, but it does mean that the question is bigger than just dict. It also involves: ChainMap Counter Mapping MutableMapping OrderedDict UserDict defaultdict at the very least. (Perhaps this is easy to implement, by just adding this to the Mapping ABC and letting everything else inherit from that. But even so, it increases the complexity of the entire Mapping ABC and all its classes.) But a bigger problem with making this a built-in dict method is deciding exactly what it should do. Here are some options: - should the method return a new dict, or modify the existing dict? - should it keep the "interesting keys" or remove them? - is it an error if one of the interesting keys is missing? - or should it be silently skipped? - or automatically added? using what value? Whatever options we pick here, you can be sure that some people will want a different set of options. Unless we are sure that one combination is much more common than the other combinations, we're better off letting people write their own helper functions that behave exactly as they want: def subset(d, keys): # Version which raises if any of the keys are missing return {key: d[key] for key in keys} -- 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: > > 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)'. On 10/12/2016 12:06 PM, Enguerrand Pelletier wrote: > b = {k, v for k,v in a.items() if k in interesting_keys} >> > Just my own opinion, I don't pretty much get you on adding a method generating Sets to the dictionary object. Best regards, Ares Ou *Software Engineer / Full-Stack Python Developer* *Blog:* http://aresou.net | *Github:* https://github.com/aresowj *Stack Overflow:* http://stackoverflow.com/users/5183727/ares-ou Ares Ou On Wed, Oct 12, 2016 at 3:03 PM, Terry Reedy wrote: > 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/ > ___ 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/
Re: [Python-ideas] Add a method to get the subset of a dictionnary.
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/