1
$\begingroup$

Trying to ReplaceAll with a list of parameters, but I can't see how to do it properly:

(a + b + c) /. {{a, b, c} -> {1, 2, 3}}
(a + b + c) /. {a, b, c} -> {1, 2, 3}

I would like to get the answer = 6, but instead I get

a + b + c

What is the correct way to pass these values as a list? I know I can do this:

(a + b + c) /. {a->1, b->2, c->3}

but I have a very long list to pass.

$\endgroup$
5
  • 8
    $\begingroup$ (a + b + c) /. Thread[{a, b, c} -> {1, 2, 3}] $\endgroup$ – cvgmt Sep 5 '20 at 1:33
  • 4
    $\begingroup$ For very long rule lists also look int Dispatch $\endgroup$ – Vitaliy Kaurov Sep 5 '20 at 4:08
  • 1
    $\begingroup$ I wonder about the close-reason: If you look up ReplaceAll or Rule in the docs, or even the tutorial Transformation Rules and Definitions, which is the extent of what seems reasonable to expect, you cannot find this problem solved. It's a commonly solved problem (google "Thread[vars ->"), and perhaps... $\endgroup$ – Michael E2 Sep 5 '20 at 15:53
  • 2
    $\begingroup$ ...here's a duplicate: mathematica.stackexchange.com/questions/42224/… $\endgroup$ – Michael E2 Sep 5 '20 at 15:53
  • $\begingroup$ The solution by @cvgmt works for me. I hadn't spotted the duplicate question. This is a good example of where a more thorough search would have avoided a duplicate post. Also the solutions below are nice use of Inner. $\endgroup$ – DrBubbles Sep 8 '20 at 19:08
2
$\begingroup$
keys = {a, b, c};
values = {1, 2, 3}; 
Flatten@Inner[Rule, {keys}, Transpose@{values}, List]
 (* {a -> 1, b -> 2, c -> 3} *)

(a + b + c) /. %
 (* 6 *)
$\endgroup$
1
  • 2
    $\begingroup$ Inner[Rule, keys, values, List] also works. $\endgroup$ – kglr Sep 5 '20 at 14:28

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