Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #67082

Re: intersection, union, difference, symmetric difference for dictionaries

Date 2014-02-25 20:21 -0600
From Tim Chase <python.list@tim.thechases.com>
Subject Re: intersection, union, difference, symmetric difference for dictionaries
References <G57Pu.24239$Th2.4990@tornado.fastwebnet.it> <CANc-5UwjkYwKJ5cqvu6c1XNG=vBuGimxuueBBFXUyuEbYoVNmw@mail.gmail.com> <mailman.7367.1393362209.18130.python-list@python.org> <530d22e5$0$29985$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.7382.1393381241.18130.python-list@python.org> (permalink)

Show all headers | View raw


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


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

intersection, union, difference, symmetric difference for dictionaries mauro <mauro@gmail.com> - 2014-02-25 20:32 +0000
  Re: intersection, union, difference, symmetric difference for dictionaries Nick Timkovich <prometheus235@gmail.com> - 2014-02-25 14:37 -0600
  Re: intersection, union, difference, symmetric difference for dictionaries Skip Montanaro <skip@pobox.com> - 2014-02-25 14:40 -0600
  Re: intersection, union, difference, symmetric difference for dictionaries Peter Otten <__peter__@web.de> - 2014-02-25 21:53 +0100
  Re: intersection, union, difference, symmetric difference for dictionaries Peter Otten <__peter__@web.de> - 2014-02-25 21:58 +0100
  Re: intersection, union, difference, symmetric difference for dictionaries Tim Chase <python.list@tim.thechases.com> - 2014-02-25 15:03 -0600
    Re: intersection, union, difference, symmetric difference for dictionaries Duncan Booth <duncan.booth@invalid.invalid> - 2014-02-25 22:21 +0000
      Re: intersection, union, difference, symmetric difference for dictionaries Tim Chase <tim@thechases.com> - 2014-02-25 16:35 -0600
      Re: intersection, union, difference, symmetric difference for dictionaries Tim Chase <python.list@tim.thechases.com> - 2014-02-25 16:36 -0600
      Re: intersection, union, difference, symmetric difference for dictionaries Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-02-25 22:58 +0000
    Re: intersection, union, difference, symmetric difference for dictionaries Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-25 23:10 +0000
      Re: intersection, union, difference, symmetric difference for dictionaries Tim Chase <python.list@tim.thechases.com> - 2014-02-25 20:21 -0600
  Re: intersection, union, difference, symmetric difference for dictionaries Ben Finney <ben+python@benfinney.id.au> - 2014-02-26 08:27 +1100
  Re:intersection, union, difference, symmetric difference for dictionaries Dave Angel <davea@davea.name> - 2014-02-25 16:35 -0500
  Re: intersection, union, difference, symmetric difference for dictionaries Peter Otten <__peter__@web.de> - 2014-02-25 22:54 +0100
  Re: intersection, union, difference, symmetric difference for dictionaries mauro <mauro@gmail.com> - 2014-02-25 22:02 +0000
  Re: intersection, union, difference, symmetric difference for dictionaries mauro <mauro@gmail.com> - 2014-02-25 22:03 +0000
  Re: intersection, union, difference, symmetric difference for dictionaries Tim Chase <python.list@tim.thechases.com> - 2014-02-25 16:10 -0600
  Re: intersection, union, difference, symmetric difference for dictionaries mauro <mauro@gmail.com> - 2014-02-25 22:11 +0000
    Re: intersection, union, difference, symmetric difference for dictionaries Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-25 22:35 +0000
  Re: intersection, union, difference, symmetric difference for dictionaries MRAB <python@mrabarnett.plus.com> - 2014-02-25 23:07 +0000
    Re: intersection, union, difference, symmetric difference for dictionaries Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-26 00:37 +0000
  Re: intersection, union, difference, symmetric difference for dictionaries Ben Finney <ben+python@benfinney.id.au> - 2014-02-26 10:14 +1100
  Re: intersection, union, difference, symmetric difference for dictionaries MRAB <python@mrabarnett.plus.com> - 2014-02-25 23:25 +0000
  Re: intersection, union, difference, symmetric difference for dictionaries Grant Edwards <invalid@invalid.invalid> - 2014-02-25 20:44 +0000
  Re: intersection, union, difference, symmetric difference for dictionaries John Gordon <gordon@panix.com> - 2014-02-25 20:44 +0000
    Re: intersection, union, difference, symmetric difference for dictionaries Chris Angelico <rosuav@gmail.com> - 2014-03-02 09:29 +1100

csiph-web