In Python 2 I can do the following:
>> d = {'a':1}
>> extras = [{'b':2}, {'c':4}]
>> map(d.update, extras)
>> d['c']
>> 4
In Python 3 in get a KeyError
:
>> d = {'a':1}
>> extras = [{'b':2}, {'c':4}]
>> map(d.update, extras)
>> d['c']
>> KeyError: 'c'
I would like to achieve the same behavior in Python 3 as in Python 2.
I understand that map in Python 3 will return an iterator (lazy evaluation and whatnot), which has to be iterated for the dictionary to be updated.
I had assumed the d['c']
key lookup would trigger the map iteration somehow, which is not the case.
Is there a pythonic way to achieve this behavior without writing a for loop, which I find to be verbose compared to map.
I have thought of using list comprehensions:
>> d = {'a':1}
>> extras = [{'b':2}, {'c':4}]
>> [x for x in map(d.update, extras)]
>> d['c']
>> 4
But it does not seem pythonic.