Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

This may be a rather strange question. I will try to create a sort of minimal working example.

Suppose that I have two lists, list1 and list2. Both list1 and list2 are ragged, and they both have multiple levels. However, list1 and list2 have the same number of elements, and their level distributions/arrangements are the same. For example:

list1 = {{{a, b, c}, {d, e, f, g, h}, {i, j}}, {{AA, BB}, {CC, DD, EE}}};
list2 = {{{1, 2, 3}, {4, 5, 6, 7, 8}, {9, 10}}, {{11, 12}, {13, 14, 15}}};

I would like to "thread" or "transpose" list1 and list2 to obtain list3, which has the same basic structure but with corresponding elements paired:

list3 = 
{{{{a, 1}, {b, 2}, {c, 3}}, {{d, 4}, {e, 5}, {f, 6}, {g, 7}, {h, 8}}, {{i, 9}, {j, 10}}},
    {{{AA, 11}, {BB, 12}}, {{CC, 13}, {DD, 14}, {EE, 15}}}}

Is there a relatively simple way to obtain list3 from list1 and list2, apart from using a complicated nesting of Table commands? Transpose, Thread, and MapThread all seem potentially useful, but not entirely straightforward for such "deep," ragged lists. Do you have any suggestions? Thanks for your time.

share|improve this question
1  
Duplicates: (6102), (9012), (17400) –  Mr.Wizard Jul 25 '13 at 16:34
    
@Mr.Wizard For this one, my memory has let me down - I was sure this wasn't asked before. Voting to close, then. –  Leonid Shifrin Jul 25 '13 at 18:06
add comment

marked as duplicate by Leonid Shifrin, Sjoerd C. de Vries, rm -rf Jul 25 '13 at 18:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 5 down vote accepted

Here is one way:

ClearAll[add];
SetAttributes[add, Listable];
add[x_, y_] := {x, y}

and now

add[list1, list2]

will give you what you want.

share|improve this answer
add comment

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