I have two lists:

a = {a1,a2,a3}
b = {b1,b2,b3}

and I want to generate a list of replacement rules

r = {a1->b1,a2->b2,a3->b3}

I would have thought I could simply do

r = #1->#2&/@[a,b]

But this produces no output. This is such a simple operation I'm sure there's something very straightforward. I could of couse use a Table, but I'm sure there's an easy and elegant method I'm missing.

  • $\begingroup$ Because /@ is Map , so one way is to #1->#2&[a,b] , but is not what you want. (#[[1]] -> #[[2]] &) /@ {{a1, a2}, {b1, b2}} partly works.Not elegant. So you may want this (#[[1]] -> #[[2]] &) /@Transpose@ {a, b} $\endgroup$ Jul 9, 2020 at 7:50

1 Answer 1

Thread[ a -> b]
 {a1 -> b1, a2 -> b2, a3 -> b3}


Inner[Rule, a, b, List]
 {a1 -> b1, a2 -> b2, a3 -> b3}


# -> #2 & @@@ Transpose[{a, b}]
 {a1 -> b1, a2 -> b2, a3 -> b3}
Rule @@@ Transpose[{a, b}]
 {a1 -> b1, a2 -> b2, a3 -> b3}

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.