Skip to main content
Active reading [<https://en.wikipedia.org/wiki/History_of_Python#Version_2> <https://en.wikipedia.org/wiki/History_of_Python#Version_3>].
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132

(For Python2Python 2.7* only; there are simpler solutions for Python3*Python 3*.)

If you're not averse to importing a standard library module, you can do

from functools import reduce

def merge_dicts(*dicts):
    return reduce(lambda a, d: a.update(d) or a, dicts, {})

(The or a bit in the lambda is necessary because dict.update always returns None on success.)

(For Python2.7* only; there are simpler solutions for Python3*.)

If you're not averse to importing a standard library module, you can do

from functools import reduce

def merge_dicts(*dicts):
    return reduce(lambda a, d: a.update(d) or a, dicts, {})

(The or a bit in the lambda is necessary because dict.update always returns None on success.)

(For Python 2.7* only; there are simpler solutions for Python 3*.)

If you're not averse to importing a standard library module, you can do

from functools import reduce

def merge_dicts(*dicts):
    return reduce(lambda a, d: a.update(d) or a, dicts, {})

(The or a bit in the lambda is necessary because dict.update always returns None on success.)

Source Link
kjo
  • 34.8k
  • 54
  • 156
  • 281

(For Python2.7* only; there are simpler solutions for Python3*.)

If you're not averse to importing a standard library module, you can do

from functools import reduce

def merge_dicts(*dicts):
    return reduce(lambda a, d: a.update(d) or a, dicts, {})

(The or a bit in the lambda is necessary because dict.update always returns None on success.)