merge namedtuples code example

Example: merge two named tuples

from collections import namedtuple
from functools import reduce


def reducer( accum, d ):
   accum.update(d)
   return accum

def  tuplemerge( *dictionaries ):
   merged = reduce( reducer, dictionaries, {} )
   return namedtuple('Merged', merged )(**merged) # <==== Gist of the gist

if __name__ == '__main__':
    obj1 = { 'a': 1, 'b': 2, 'c': 3 }
    obj2 = { 'd': 44, 'e': 55, 'f': 66 }
    obj3 = { 'g': 88, 'h': 99, 'f': 111 } # <=== this 'f' overrides previous 'f'
    print(tuplemerge( obj1,obj2,obj3 ))