6
$\begingroup$

Consider the following two lists

{1,2,3,4,5,6}

{{1},{1},{1,2},{1,3},{1,3,4},{1,3,5}}

How can I thread both lists to obtain the following three lists

{{1,1},{2,1},{3,1},{4,1},{5,1},{6,1}}
{{3,2},{4,3},{5,3},{6,3}}
{{5,4},{6,5}}
$\endgroup$
7
$\begingroup$

If your first list has the form Range[n] then

b = {{1}, {1}, {1, 2}, {1, 3}, {1, 3, 4}, {1, 3, 5}};
Flatten[MapIndexed[{First[#2], #} &, b, {2}], {{2}, {1}}]
{{{1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}},  
 {{3, 2}, {4, 3}, {5, 3}, {6, 3}},  
 {{5, 4}, {6, 5}}}

Alternatively:

a = {1, 2, 3, 4, 5, 6};
Flatten[MapThread[Thread@*List, {a, b}], {{2}, {1}}]
$\endgroup$
  • $\begingroup$ Can u generalize this answer with that simplicity to any type of list with single numbered values as its elements? $\endgroup$ – jarhead May 5 '18 at 14:07
3
$\begingroup$
a = {1, 2, 3, 4, 5, 6};
b = {{1}, {1}, {1, 2}, {1, 3}, {1, 3, 4}, {1, 3, 5}};

Inner[Thread[{##}] &, a, b, Flatten[{##}, {{2}, {1}}] &]

{{{1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}},
{{3, 2}, {4, 3}, {5, 3}, {6, 3}},
{{5, 4}, {6, 5}}}

Also

f1 = Flatten[#, {{2}, {1}}] &;
f2 = Thread /@ # &;
Composition[f1, f2, f1]@{a, b}

{{{1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}},
{{3, 2}, {4, 3}, {5, 3}, {6, 3}},
{{5, 4}, {6, 5}}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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