What is the simple way to merge named tuples in Python?
Some observations:
In general Python would not know what to do when you try to merge two namedtuples that happen to have fields with the same name. Perhaps this is why there is no operator or function for this.
The documentation of
_fields
says:
Tuple of strings listing the field names. Useful for introspection and for creating new named tuple types from existing named tuples.
This suggests your approach is fine and perhaps even hinted at by the authors of the namedtuple code.
You've pretty much nailed it as far as vanilla Python is concerned, but there's an extra simplification you can do if you're using Python 3.5+.
>>> from collections import namedtuple
>>> A = namedtuple("A", "a b c")
>>> B = namedtuple("B", "d e")
>>> a = A(10, 20, 30)
>>> b = B(40, 50)
>>> C = namedtuple("C", A._fields + B._fields)
>>> C(*(a + b))
C(a=10, b=20, c=30, d=40, e=50)
>>> #Available in Python 3.5+
>>> C(*a, *b)
C(a=10, b=20, c=30, d=40, e=50)
Also, here's a function you can use to eliminate boilerplate code if you find yourself doing this frequently:
>>> from functools import reduce
>>> from itertools import chain
>>> from operator import add
>>> def namedtuplemerge(*args):
... cls = namedtuple('_'.join(arg.__class__.__name__ for arg in args), reduce(add, (arg._fields for arg in args)))
... return cls(*chain(*args))
...
>>> namedtuplemerge(a, b)
A_B(a=10, b=20, c=30, d=40, e=50)
Python will not automagically create a new namedtuple
, a new class. You will have to define the combined namedtuple
yourself:
A = namedtuple("A", "a b c")
B = namedtuple("B", "d e")
AB = namedtuple("AB", "a b c d e")
a = A(1,2,3)
b = B(4,5)
ab = AB(*(a+b))
>>> a
A(a=1, b=2, c=3)
>>> b
B(d=4, e=5)
>>> ab
AB(a=1, b=2, c=3, d=4, e=5)