3
$\begingroup$

I have a length-N list of rules

l = {a -> 5, b -> 1, c -> 42}

and an length-N vector of scalars...

dl = {-1, .2, 5}

which I'd like to add to the RHS of rules, to produce

{a -> 5 -1, b -> 1 +.2, c -> 42 +5}

I can currently achieve this verbosely with

Table[l[[t, 1]] -> l[[t, 2]] + dl[[t]], {t, Length[l]}]

but I'm looking for the most concise method possible.

$\endgroup$
4
$\begingroup$

You could use Part with AddTo:

l = {a -> 5, b -> 1, c -> 42};
dl = {-1, .2, 5};

l[[All, 2]] += dl;
l

{a -> 4, b -> 1.2, c -> 47}

| improve this answer | |
$\endgroup$
  • $\begingroup$ Fantastic, thanks very much! $\endgroup$ – Anti Earth Dec 3 '19 at 19:08

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.