# Using a list of rules to update its own values

Suppose I have a list of rules with a mix of values and equations on the RHS.

eqn={a->1,x->a+y,y->b,b->2}


How can I update the list such that RHS is evaluated using all the rules (assuming the list of rules is exhaustive and covers all variables)? This is what I want:

eqn={a->1,x->3,y->2,b->2}


I can do

Thread[Rule[eqn[[;; , 1]], eqn[[;; , 2]] //. eqn]]


But it looks too messy. Is there a simple function for this?

• Since the new rule covers all variables,why not use the new rule directly? – yode Apr 17 '17 at 22:30
• @yode I don't have the new rule. I want to obtain the new rule by applying the old rule to itself. – Stitch Apr 17 '17 at 22:31
• MapAt[# //. eqn &, eqn, {All, 2}] comes to mind. – C. E. Apr 17 '17 at 22:32
• @C.E. Ahh, MapAt of course! Great! – Stitch Apr 17 '17 at 22:37

MapAt[# //. eqn &, eqn, {All, 2}]


{a -> 1, x -> 3, y -> 2, b -> 2}

or

# -> (#2 //. eqn) & @@@ eqn


{a -> 1, x -> 3, y -> 2, b -> 2}

• All other solutions are very nice too, but this is the fastest one, especially for messy lists. Also, it is the only one that doesn't eliminate the duplicates in the list and doesn't alter the order (not important, but good to have). – Stitch Apr 18 '17 at 2:30
• But you have not stated you hope to reserve those duplicates elements. :) – yode Apr 18 '17 at 2:46
• @yode The question is pretty clear on that actually, "how can I update the list such that RHS is evaluated using all the rules" is not the same as "How can I update the list such that RHS is evaluated using all the rules and deleete duplicates". Also OP's solution does not delete duplicates. – C. E. Apr 18 '17 at 10:07
• @yode The main reason this is the fastest solution -- the real list is much longer and is called many times, so the speed is important. The duplicates are nice, but not a deal breaker :) – Stitch Apr 18 '17 at 16:48
Normal[<|eqn|>//.eqn]


{a->1,x->3,y->2,b->2}

Solve[Equal @@@ eqn][[1]]


{a -> 1, b -> 2, x -> 3, y -> 2}

Or

List @@ Rule @@@ Reduce[Equal @@@ eqn]


{y -> 2, x -> 3, b -> 2, a -> 1}

• +1)..Good lesson for Reduce and Solve. :) – yode Apr 17 '17 at 22:49
• That's pretty nice! Didn't think about solving it in such an elegant way! – Stitch Apr 17 '17 at 22:53
• @yode, Stitch thank you both for the votes. – kglr Apr 17 '17 at 22:58