Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(at': 0.04; 'resulting': 0.04; 'duplicate': 0.07; 'to:addr:pobox.com': 0.09; 'to:addr:skip': 0.09; 'way:': 0.09; 'cc:addr:python-list': 0.11; '-tkc': 0.16; 'evaluating': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'least)': 0.16; 'lhs': 0.16; 'readable': 0.16; 'rhs': 0.16; 'subject: \n ': 0.16; 'subject:dictionaries': 0.16; "{'a':": 0.16; 'wrote:': 0.18; 'result.': 0.19; 'cc:addr:gmail.com': 0.22; 'otherwise,': 0.22; 'preferred': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**1': 0.23; 'propose': 0.24; 'skip': 0.24; 'earlier': 0.24; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'sets': 0.30; '(on': 0.31; 'keys': 0.31; 'operations': 0.35; 'but': 0.35; "didn't": 0.36; 'charset:us-ascii': 0.36; 'possible': 0.36; 'two': 0.37; 'being': 0.38; 'space': 0.40; 'save': 0.62; 'choose': 0.64; 'week,': 0.64; 'received:50.22': 0.84 Date: Tue, 25 Feb 2014 15:03:51 -0600 From: Tim Chase To: Skip Montanaro Subject: Re: intersection, union, difference, symmetric difference for dictionaries In-Reply-To: References: 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 Cc: Python , mauro 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393362209 news.xs4all.nl 2858 [2001:888:2000:d::a6]:32969 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67060 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 set(['A']) # you did set-ops, so you get a set If dicts were to support set ops, the last one would be my preferred result. I just had to perform set operations on a pair of dicts earlier this week, shrugged, and did things the manual/explicit way: a_dict = dict(...) b_dict = dict(...) a_set = set(a_dict) b_set = set(b_dict) added_keys = b_set - a_set removed_keys = a_set - b_set same_keys = a_set & b_set diff_keys = a_set ^ b_set all_keys = a_set | b_set It would save some space if I didn't have to duplicate all the keys into sets (on the order of 10-100k small strings), instead being able to directly perform the set-ops on the dicts. But otherwise, it was pretty readable & straight-forward. -tkc