# How to use Map inside MapThread?

Given this input

lst1 = {{a, b, c}, {d, e, f}};
lst2 = {1, 2};


and the goal is to generate this output

{  {{1, a}, {1, b}, {1, c}},
{{2, d}, {2, e}, {2, f}}
}


Perfect candidate for MapThread So I made this diagram first to figure what the function I want to map should be

So the function to use inside MapThread, needs to also use Map itself (in order to map each item into the other list). So I came up with this:

lst1 = {{a, b, c}, {d, e, f}};
lst2 = {1, 2};
foo[i_, lst_List] := List[i, #] & /@ lst
(*  {  {{1,a}, {1,b}, {1,c}},       {{2,d}, {2,e}, {2,f}}   }  *)


Now here is the question: Is there a way to do the above without having to define an explicit function but using pure function inside MapThread?

I was getting conflict with # mapping. This is sort of the thing I was trying to do, but can't get the syntax right

(*invalid, for illustration only *)
Function[{idx, lst},List[idx, #] & /@ lst] &  ?? ??    ,{Range[Length@lst2],lst1}]


Or if you know of a better approach to do this, that will be fine as well.

• Get rid of that last & in your illustration as you already have the long form of using Function[ ]. You can also use Thread[{##}] & as the function for MapThread
– ssch
Nov 29 '13 at 18:38
• Thanks, I guess I was close :) can you show how to use {##}? I am not good with ##, I know # only now. Nov 29 '13 at 18:46

MapThread[Thread[{##}] &, {lst2, lst1}]



## is used so Thread gets called like Thread[{1, {a, b, c}}] As MapThread gives two arguments in this case it is equivalent to Thread[{#1, #2}]& and Composition[Thread, List]

• Thread[{##}] is nice since it is shorter. I have to read about it, as never saw it before. Nov 29 '13 at 18:51
• V10 style: MapThread[Thread@*List, {lst2, lst1}]
– Kuba
Apr 18 '16 at 8:30

Very similar to ssch's second answer, but sometimes Thread feels more natural than Transpose:

Thread /@ Thread @ {lst2, lst1}


Less clear, but more interesting, is to make a Listable version of List:

Function[, {##}, Listable][lst2, lst1]


You could also use my smartThread function:

smartThread @ {lst2, lst1}

• smartThread is so cool. I did not know about it, will start using it. I wish Mathematica had such functions build in. I am starting to think the we need a major overhaul of number of Mathematica core functions to add more options to them and make them more flexible. Sometimes I feel it takes too much work and skill to do some basic manipulation of lists. Nov 29 '13 at 23:20

A Map/MapThread-less solution

Transpose@Inner[List, lst2, lst1, List]


An Outer version:

Flatten[ MapThread[ Outer[List, {#1}, #2] &, {lst2, lst1}], 1]


{{{1, a}, {1, b}, {1, c}}, {{2, d}, {2, e}, {2, f}}}

MapThread[Function[{u, b}, List[b, #] & /@ u], {lst1, lst2}];


and the reverse example with Slots on top:

MapThread[Map[Function[u, {#2, u}], #] &, {lst1, lst2}]

{{{1, a}, {1, b}, {1, c}}, {{2, d}, {2, e}, {2, f}}}


Approach with MapIndexed:

MapIndexed[{lst2[[#2[[1]]]], #} &, lst1, {2}]

MapThread[With[{n = #2}, {n, #} & /@ #1] &, {lst1, lst2}]


{{{ 1, a}, {1, b}, {1, c}}, {{2, d}, {2, e}, {2, f}}}

Why MapThread?

While both MapThread and in particular MapIndexed are slow, using some Transpose based constructions, one can take Map (or even ParallelMap, if appropriate):

Map[Function[v, Map[{v[[2]], #} &, v[[1]]]][#] &, Transpose[Join[{lst1, lst2}]]]


Edit: I suddenly noticed that cascading Map was a stupid idea, so here my correction:

Map[
Function[v, Transpose@{ConstantArray[v[[2]], Length[v[[1]]]], v[[1]]}
][#] &, Transpose[Join[{lst1, lst2}]]
]


In particular for large lists as usually can occur in image analysis, trying to avoid MapThread and MapIndexed is often recommended.

Using Table,

Thread /@ Table[{lst2[[k]], lst1[[k]]}, {k, Length[lst1]}]


{{{1, a}, {1, b}, {1, c}}, {{2, d}, {2, e}, {2, f}}}