Suppose I have two lists each of different sizes, e.g. toy case ListX = {x1, x2, x3} and ListY = {y1, y2} and all possible combinations of elements give rise to an element in another list ListZ = {z1, z2, z3, z4, z5, z6} of length Length[ListX] $ \times $ Length[ListY], i.e the tuple (x1, y1) is associated with z1, say.

How to merge all three lists such that I obtain the following combined list?

{{{x1, y1}, z1}, {{x1, y2}, z2}, {{x2, y1}, z3}, 
 {{x2, y2}, z4}, {{x3, y1}, z5}, {{x3, y2}, z6}}

I've tried nested tables but I always generate additional items that I don't want. The actual lists I'm dealing with are of length O(50) such that the equivalent of ListZ is of length O(2500).


2 Answers 2


This seems to do what you are looking for:

ListX = {x1, x2, x3};
ListY = {y1, y2};
ListZ = {z1, z2, z3, z4, z5, z6};
Partition[Riffle[Flatten[Outer[List, ListX, ListY], 1], ListZ], 2]

The Outer product gives all the pairs, but they are nested funny so you need to Flatten. Riffle does the interleaving of the Outer product with ListZ, and again it's not quite got your desired structure, but Partition fixes that.

Transpose@{Tuples@{ListX, ListY}, ListZ}
  • 1
    $\begingroup$ Very nice solution! You can also use Thread instead of Transpose:Thread@{Tuples@{ListX, ListY}, ListZ} $\endgroup$
    – rmw
    Feb 24, 2019 at 18:57
  • $\begingroup$ Yes. Thanks for reminding me that. $\endgroup$
    – ukar
    Feb 24, 2019 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.