Reputation: 1124
I was trying to update values in my dictionary, I came across 2 ways to do so:
product.update(map(key, value))
product.update(key, value)
What is the difference between them?
Upvotes: 31
Views: 89568
Reputation: 62403
dict union
, for updating one dict
from another dict
.
dict
consisting of the left operand merged with the right operand, each of which must be a dict
(or an instance of a dict
subclass). If a key appears in both operands, the last-seen value (i.e. that from the right-hand operand) wins.>>> d = {'spam': 1, 'eggs': 2, 'cheese': 3}
>>> e = {'cheese': 'cheddar', 'aardvark': 'Ethel'}
>>> d | e
{'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'}
>>> e | d
{'aardvark': 'Ethel', 'spam': 1, 'eggs': 2, 'cheese': 3}
The current ways to merge two dicts have several disadvantages:
d1.update(d2)
modifies d1
in-place. e = d1.copy()
; e.update(d2)
is not an expression and needs a temporary variable.
Dict unpacking looks ugly and is not easily discoverable. Few people would be able to guess what it means the first time they see it, or think of it as the "obvious way" to merge two dicts.
Upvotes: 5
Reputation: 1121744
The difference is that the second method does not work:
>>> {}.update(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: update expected at most 1 arguments, got 2
dict.update()
expects to find a iterable of key-value pairs, keyword arguments, or another dictionary:
Update the dictionary with the key/value pairs from other, overwriting existing keys. Return
None
.
update()
accepts either another dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key/value pairs:d.update(red=1, blue=2)
.
map()
is a built-in method that produces a sequence by applying the elements of the second (and subsequent) arguments to the first argument, which must be a callable. Unless your key
object is a callable and the value
object is a sequence, your first method will fail too.
Demo of a working map()
application:
>>> def key(v):
... return (v, v)
...
>>> value = range(3)
>>> map(key, value)
[(0, 0), (1, 1), (2, 2)]
>>> product = {}
>>> product.update(map(key, value))
>>> product
{0: 0, 1: 1, 2: 2}
Here map()
just produces key-value pairs, which satisfies the dict.update()
expectations.
Upvotes: 53