What you want is reduce
, not map
. map
creates a sequence by applying a function to each element of the given sequence, whereas reduce
accumulates an object over the sequence.
from functools import reduce
inp = [{1:100}, {3:200}, {8:300}]
dct = {}
reduce(lambda d1, d2: (d1.update(d2) or d1), inp, dct)
{1: 100, 3: 200, 8: 300}
This lambda
part may look tricky. What this does is to create a function that updates a dict and returns the dictionary itself. This is because reduce
expects the accumulating function returns the updated object while dict.update()
returns None
. Since None
is interpreted as False, the or
operator returns the second variable, which is the updated dictionary.
This or
trick is credited to @kostya-goloveshko in this thread: Why doesn't a python dict.update() return the object?