4
$\begingroup$

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.

$\endgroup$
1
  • $\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

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

Also:

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

and

# -> #2 & @@@ Transpose[{a, b}]
 {a1 -> b1, a2 -> b2, a3 -> b3}
Rule @@@ Transpose[{a, b}]
 {a1 -> b1, a2 -> b2, a3 -> b3}
$\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.