5 ms·
Unrelated to python 3.7, but they recently added this feature that you can add two dictionaries together in 3.5 by doing: new_dict = {**dict1, **dict2} It i
by Kpourdeilami 9y ago
Unrelated to python 3.7, but they recently added this feature that you can add two dictionaries together in 3.5 by doing:
new_dict = {**dict1, **dict2}
It is so handy and nice
- agumonkey 9y agoI didn't know about that. It's also unbounded: >>> d = {**{1:2,2:2,3:2} ,**{4:2,5:2,6:2} ,**{7:2,8:2,9:2}} >>> d { 1: 2, 2: 2, 3: 2 , 4: 2, 5: 2, 6: 2 , 7: 2, 8: 2, 9: 2}
- JupiterMoon 9y agoWhat happens to duplicate keys?
- dave_sullivan 9y agoI think it raises an exception. https://www.python.org/dev/peps/pep-0448/ https://www.python.org/dev/peps/pep-0448/ EDIT: yeah, nvm, it's what everyone else said.
- skrause 9y agoNo it doesn't, you simply get the value from the last dict: >>> a = {'x': 1} >>> b = {'x': 2} >>> {**a, **b} {'x': 2} >>> {**b, **a} {'x': 1}
- Kpourdeilami 9y agoThey get overwritten by the value of the dictionary that came last. >>> dict1 = {"key1": 1, "key2": "hello", "key3": [1, 2, 3]} >>> dict2 = {key1": 1, "key2": "world", "key3": [1, 4], "key4": "4"} >>> {**dict1, **dict2} {'key1': 1, 'key2': 'world', 'key3': [1, 4], 'key4': '4'}
- ericfrederich 9y agoLots of code to show that. How 'bout this: ;-) >>> {**{'a': 'first'}, **{'a': 'second'}} {'a': 'second'}
- masklinn 9y agoLast pair wins, same as setting an existing key or doing an update() with an existing key.
- joelthelion 9y agoAh, great. This used to be rather clunky to do.
- saurik 9y agoReally? This is how that was done in Python 2.7 and it doesn't seem that much clunkier to me... dict(dict1, **dict2)
- heinrich5991 9y agoThat's only allowed if the keys in the second dictionary are valid Python identifiers (don't contain spaces etc.). I think CPython is OK with this, but PyPy is not.
- snthpy 9y agoInteresting. I never thought of that. I also asked a question about this on StackOver flow once and no one suggested that. Very useful.
- joelthelion 9y agoIt has a lot of issues. The biggest one for me is the following: Dicts are intended to take hashable keys (e.g. frozensets or tuples), but this method fails in Python 3 when keys are not strings. Source: https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression https://stackoverflow.com/questions/38987/how-to-merge-two-d...
- jcrben 9y agoLooks like this came from https://www.python.org/dev/peps/pep-0448/ https://www.python.org/dev/peps/pep-0448/ Seems similar to the spread operator in JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator#Spread_in_object_literals https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- martinvol 9y agoIt's fun that I use it every day but my linter still thinks is a syntax error :)
- ericfrederich 9y agoA linter for a dynamic language has got to be the hardest thing ever to implement. In PyCharm if you return from within a contextlib.suppress context it'll think any code following it is unreachable code. https://youtrack.jetbrains.com/issue/PY-25809 https://youtrack.jetbrains.com/issue/PY-25809 https://youtrack.jetbrains.com/issue/PY-16419 https://youtrack.jetbrains.com/issue/PY-16419
- mixmastamyk 9y agoWhy doesn't the + symbol work?
- Kpourdeilami 9y ago+ is not a supported operand for dict. You can probably subclass dict and add the plus operand
- mixmastamyk 9y agoWhy add that crazy syntax when a + would be a lot clearer?
- akvadrako 9y agoIt makes sense in context. {} is a dict literal, and * * dict is already used other places to expand a dict into key-value pairs. Given a = {1: 1} b = {2: 2} This c = {**a, **b} is equivalent to c = {1: 1, 2: 2}
- namelost 9y agoa|b would be the obvious choice, because it already works for set: {1,2}|{2,3} == {1,2,3} I guess the reason they went for the weird {∗∗a, ∗∗b} syntax is that it is not a commutative operation.
- mixmastamyk 9y agoWouldn't mind that either.
- akvadrako 9y agoI guess I didn't explain it clearly enough. ∗∗a is not new syntax, it's just applicable in a new context.
- ericfrederich 9y ago