Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(at': 0.04; 'resulting': 0.04; '(except': 0.07; 'used.': 0.09; 'python': 0.11; '"or"': 0.16; '*default*': 0.16; '-tkc': 0.16; 'behave': 0.16; 'defaulting': 0.16; 'evaluating': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'inconvenient': 0.16; 'least)': 0.16; 'lhs': 0.16; 'mutable': 0.16; 'prefer.': 0.16; 'rhs': 0.16; 'set,': 0.16; 'similarly,': 0.16; 'subject: \n ': 0.16; 'subject:dictionaries': 0.16; 'tuple,': 0.16; 'write,': 0.16; "{'a':": 0.16; 'exception': 0.16; 'wrote:': 0.18; 'result.': 0.19; 'feb': 0.22; '(in': 0.22; 'preferred': 0.22; '2.x': 0.24; 'certainly': 0.24; 'helper': 0.24; 'propose': 0.24; 'skip': 0.24; 'pass': 0.26; 'least': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'record': 0.27; 'function': 0.29; 'correct': 0.29; 'fixed': 0.29; 'possibility': 0.29; 'raise': 0.29; 'tim': 0.29; "i'm": 0.30; 'getting': 0.31; 'chase': 0.31; 'commonly': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'option': 0.32; 'another': 0.32; 'quite': 0.32; "i'd": 0.34; 'no,': 0.35; 'operations': 0.35; 'but': 0.35; 'add': 0.35; 'really': 0.36; 'charset:us-ascii': 0.36; 'thanks': 0.36; 'possible': 0.36; 'should': 0.36; 'behind': 0.37; 'two': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'does': 0.39; 'skip:. 10': 0.39; 'to:addr:python.org': 0.39; 'easy': 0.60; 'most': 0.60; 'simple': 0.61; 'choose': 0.64; 'results': 0.69; 'back?': 0.84; 'different.': 0.84; 'received:50.22': 0.84; 'do:': 0.91; 'promotion,': 0.91; 'imagine': 0.93 Date: Tue, 25 Feb 2014 20:21:08 -0600 From: Tim Chase To: python-list@python.org Subject: Re: intersection, union, difference, symmetric difference for dictionaries In-Reply-To: <530d22e5$0$29985$c3e8da3$5496439d@news.astraweb.com> References: <530d22e5$0$29985$c3e8da3$5496439d@news.astraweb.com> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393381241 news.xs4all.nl 2904 [2001:888:2000:d::a6]:41266 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67082 On 2014-02-25 23:10, Steven D'Aprano wrote: > On Tue, 25 Feb 2014 15:03:51 -0600, Tim Chase wrote: > > > On 2014-02-25 14:40, Skip Montanaro wrote: > >> What's the correct result of evaluating this expression? > >> > >> {'A': 1} | {'A': 2} > >> > >> I can see (at least) two possible "correct" answers. > > > > I would propose at least four: > > > > {'A': 1} # choose the LHS > > {'A': 2} # choose the RHS > > {'A': (1,2)} # a resulting pair of both > > Should that value be a tuple, a list or a set? I'd say a tuple: it has order (thus not a set), and it's a fixed record of (LHS, RHS), not mutable as a list would suggest. > Option 5: raise an exception if the values are different. Inconvenient in many use cases, but certainly another possibility > Option 6: "or" the values, so that the LHS value is used only if it > is truthy, otherwise the RHS value is used. That is: > > {'A': leftdict['A'] or rightdict['A']} > > > I don't really understand the use-case behind Option 6 I can imagine a use case for it, but certainly not the *default* behavior. Yick. > > If dicts were to support set ops, the last one would be my > > preferred result. > > What, getting a set back? > > No, I disagree. If you want a set, it's easy to do: > > dicta.keys() | dictb.keys() > > (In Python 2, use viewkeys instead.) Now that I'm aware of .viewkeys()/.keys() (herein "vk/k" for brevity) thanks to Peter Otten's post, that does what I most commonly want for set-ops-on-dicts. I'd just consider this a promotion, since for k in my_dict: pass is the same as for k in my_dict.keys(): # or .iterkeys() in 2.x pass I'd find it intuitive to have set-ops behave similarly, defaulting to vk/k. > All of these operations are quite simple to write, so personally I > would recommend people just add their own helper function with the > behaviour they prefer. And with vk/k, I now fall pretty firmly in agreement with you (except for the aforementioned possibility of having a dict's currently-unused set-ops use the results of vk/k as well). -tkc