Creating NamedTuple from exisiting NamedTuples using their keys and values?

I’m looking to combine two NamedTuples in Julia in the following way -

I start with p = (a = 1, b = 2) and q = (c = 3, d = 4). What I want is to create a new NamedTuple with the keys of p and the values of q (or vice versa), for example, r = (a = 3, b = 4). What would be the simplest way to do this?

So far, I’ve been exploring keys(p) = (:a, :b) and values(q) = (3, 4) but I’m not sure how to combine them.

NamedTuple{keys(p)}(values(q)) should do the trick.

1 Like

Yes this works, thanks!