Message 373205 - Python tracker

Message373205

Author Raps Uk
Recipients Aaron Hall, Raps Uk, brandtbucher, chaburkland, curtisbucher, gvanrossum, josh.r, justjais, mark.dickinson, rhettinger, scoder, serhiy.storchaka, slam, steve.dower, xtreak
Date 2020-07-07.05:39:40
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1594100380.56.0.0311857034129.issue36144@roundup.psfhosted.org>
In-reply-to
Content
Taking the union of items() in Python 3 (viewitems() in Python 2.7) will also fail when values are unhashable objects (like lists, for example). Even if your values are hashable, since sets are semantically unordered, the behavior is undefined in regards to precedence. So don't do this:

>>> c = dict(a.items() | b.items())
This example demonstrates what happens when values are unhashable:

>>> x = {'a': []}
>>> y = {'b': []}
>>> dict(x.items() | y.items())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
Here's an example where y should have precedence, but instead the value from x is retained due to the arbitrary order of sets:

>>> x = {'a': 2}
>>> y = {'a': 1}
>>> dict(x.items() | y.items())
{'a': 2}

http://net-informations.com/python/ds/merge.htm
History
Date User Action Args
2020-07-07 05:39:40Raps Uksetrecipients: + Raps Uk, gvanrossum, rhettinger, mark.dickinson, scoder, serhiy.storchaka, steve.dower, josh.r, Aaron Hall, slam, xtreak, brandtbucher, curtisbucher, chaburkland, justjais
2020-07-07 05:39:40Raps Uksetmessageid: <1594100380.56.0.0311857034129.issue36144@roundup.psfhosted.org>
2020-07-07 05:39:40Raps Uklinkissue36144 messages
2020-07-07 05:39:40Raps Ukcreate