# How to get such rule with a elegant method

I have two lists:

list={{1,2,3,5},{5,3,9,11,12},{5,9,10,16}};
list2={{7,89},{96,5},{-6,-98}};


This is the expected result.

{{1->{7,89},2->{7,89},3->{7,89},5->{7,89}},
{5->{96,5},3->{96,5},9->{96,5},11->{96,5},12->{96,5}},
{5->{-6,-98},9->{-6,-98},10->{-6,-98},16->{-6,-98}}}


This is my current try.

Thread /@
MapIndexed[ConstantArray[list2[[First[#2]]], Length[#1]] &, list]]]


Tuples /@ Thread[list -> List /@ list2]

{
{1 -> {7, 89}, 2 -> {7, 89}, 3 -> {7, 89}, 5 -> {7, 89}},
{5 -> {96, 5}, 3 -> {96, 5}, 9 -> {96, 5}, 11 -> {96, 5}, 12 -> {96, 5}},
{5 -> {-6, -98}, 9 -> {-6, -98}, 10 -> {-6, -98}, 16 -> {-6, -98}}
}

• Good lesson to me.Thanks.
– yode
Commented Dec 25, 2016 at 18:58
• @yode You're welcome :^) Commented Dec 25, 2016 at 19:01
• I mean,your usage of Tuples and List/@ astonish me. :)
– yode
Commented Dec 25, 2016 at 19:04
• @yode I was just having good-natured fun with your typo. I am glad you learned from this example. Quite a few functions work on heads other than List which is quite handy, e.g. (1312) Commented Dec 26, 2016 at 3:13
• It's not a typo.I just cannot hold the temper of my English sometimes. :)
– yode
Commented Dec 26, 2016 at 3:26
MapIndexed[# -> list2[[#2[[1]]]] &, list, {2}]


or

ReplacePart[list, {i_, j_} :> list[[i, j]] -> list2[[i]]]

• Wow,thanks.This is very concise method,too.
– yode
Commented Dec 25, 2016 at 8:08
Thread /@ Thread[list -> Hold /@ list2] // ReleaseHold
Thread /@ Thread[list → Unevaluated /@ list2] /. a_ → b_ -> a -> b
Thread /@ Thread[list -> $/@ list2] /.$ -> (# &)


As to the reason why I used RightArrow as a medium in the second solution, you may want to read this post.

• +1)Impressive...BTY,how do you type that second $\to$?
– yode
Commented Dec 25, 2016 at 7:31
• @yode I just transformed \[RightArrow] with M.SE editor :) . Commented Dec 25, 2016 at 7:41
• You can also type ESC SPACE - > ESC. Commented Dec 25, 2016 at 7:54

Another way to use Thread:

Thread[#, List, 1] & /@ Thread[list -> list2]

{{1 -> {7, 89}, 2 -> {7, 89}, 3 -> {7, 89}, 5 -> {7, 89}},
{5 -> {96, 5}, 3 -> {96, 5}, 9 -> {96, 5}, 11 -> {96, 5}, 12 -> {96, 5}},
{5 -> {-6, -98}, 9 -> {-6, -98}, 10 -> {-6, -98}, 16 -> {-6, -98}}}


MapThread + Outer:

MapThread[Outer[Rule, ##, 1] &, {list, List /@ list2}]


Obfuscatory:

or

o = (\[FivePointedStar] \[Function] \[FivePointedStar] -> #2) /@ # & @@@ ({##}\[Transpose]) &;
o[list, list2]

• Upvote for Star. :)
– yode
Commented Dec 25, 2016 at 18:59
MapThread[Map[Function[x, Rule[x, #2]], #1] &, {list, list2}]

• Joke version: MapThread[(#0[[0]] @@ {#0[[1, 2]] -> #2}) /@ # &, {list, list2}]  Commented Dec 28, 2016 at 15:12

I'm late to this party, but I don't see any other answer like this:

 helper[{u_, v_}] := Rule[#, v] & /@ u
helper /@ Transpose[{list, list2}]

{{1 -> {7, 89}, 2 -> {7, 89}, 3 -> {7, 89}, 5 -> {7, 89}},
{5 -> {96, 5}, 3 -> {96, 5}, 9 -> {96, 5}, 11 -> {96, 5}, 12 -> {96, 5}},
{5 -> {-6, -98}, 9 -> {-6, -98}, 10 -> {-6, -98}, 16 -> {-6, -98}}}


This seems to me to be a nice, simple way of solving the problem.

This works, and it is easy to understand:

Flatten[MapThread[ Function[{l1, l2}, Map[{# -> l2} &, l1]], {list, list2}], 1]