7
$\begingroup$

I have a list of equations that were solved for different values of another parameter and stored in a Table and has the form:

{a==0, a==1, a==2, a==3}

and so on. I want to withdraw only in rhs and store it in a list:

{0, 1, 2, 3}

I tried /.ToRules on the whole list as well as on the single elements because I thought, they could be evaluated easier. I expected {a->0,a->1,a->2,a->3} but it's giving me the same error for either one: "{ToRules} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing"

$\endgroup$
2
  • 1
    $\begingroup$ Thanks for accepting a good answer, however I think you were too hasty doing that. While accepting is one of the things to do after your question is answered, we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all time zones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers. $\endgroup$
    – rhermans
    Commented Feb 27, 2023 at 16:39
  • $\begingroup$ Ok, that makes it 10+ different solutions then. $\endgroup$
    – rhermans
    Commented Feb 27, 2023 at 16:50

7 Answers 7

5
$\begingroup$

Perhaps

{a == 0, a == 1, a == 2, a == 3}[[All, 2]]
(*{0, 1, 2, 3}*)

or

{a == 0, a == 1, a == 2, a == 3} /.Equal->Rule
(*{a -> 0, a -> 1, a -> 2, a -> 3}*)
$\endgroup$
2
  • 3
    $\begingroup$ fyi, you have typo. Rules should be Rule $\endgroup$
    – Nasser
    Commented Feb 27, 2023 at 15:07
  • $\begingroup$ @Nasser Thanks for your hint $\endgroup$ Commented Feb 27, 2023 at 16:05
5
$\begingroup$

Using MapApply and Values:

Rule @@@ lis

(*{a -> 0, a -> 1, a -> 2, a -> 3}*)

Values@(Rule @@@ lis)

(*{0, 1, 2, 3}*)
$\endgroup$
5
$\begingroup$

Using ToRules

You wanted to use ToRules. You could have Map (/@) it like this

ToRules/@{a == 0, a == 1, a == 2, a == 3}
(* {{a->0},{a->1},{a->2},{a->3}} *)

So a complete solution

Composition[First, Values, ToRules] /@ {a == 0, a == 1, a == 2, a == 3}
(*{0, 1, 2, 3}*)

or

First@*Values@*ToRules /@ {a == 0, a == 1, a == 2, a == 3}
(*{0, 1, 2, 3}*)

Using MapApply Last

Last @@@ {a == 0, a == 1, a == 2, a == 3}
(*{0, 1, 2, 3}*)

This is a hack that relies on the LHS (in this case a) been AtomQ.

Using Cases

Cases[
    {a == 0, a == 1, a == 2, a == 3}
    ,  Equal[lhs_,rhs_]:> rhs
]
(*{0, 1, 2, 3}*)

Using ReplaceAll

{a == 0, a == 1, a == 2, a == 3} /. Equal[lhs_,rhs_]:> rhs
(*{0, 1, 2, 3}*)

or in operator mode

ReplaceAll[ Equal[lhs_,rhs_]:> rhs] @ {a == 0, a == 1, a == 2, a == 3}
(*{0, 1, 2, 3}*)
$\endgroup$
4
$\begingroup$

Another possibility

lis = {a == 0, a == 1, a == 2, a == 3}
Last /@ lis

Mathematica graphics

In an equation Last is the RHS and First is the LHS.

If you want to make it Rule, I think Ulrich answer might be the easiest, which is to replace == by Rule. But you can also do

lis = {a == 0, a == 1, a == 2, a == 3}
Rule[First[#], Last[#]] & /@ lis

Mathematica graphics

$\endgroup$
4
$\begingroup$

One possible way using Values:

lis = {a == 0, a == 1, a == 2, a == 3};
lis /. Equal -> Rule // Values

{0, 1, 2, 3}

$\endgroup$
3
$\begingroup$

Since you asked about rules, the following will replace each value appropriately

MapThread[ReplaceAll, {Table[a, {i, 1, Length@l}], 
  l /. Equal -> Rule}]

{0,1,2,3}

$\endgroup$
2
$\begingroup$
list = {a == 0, a == 1, a == 2, a == 3};

Using MapApply (new in 13.1)

MapApply[Last] @ list

Using ReplaceAt (new in 13.1)

ReplaceAt[_ :> Last, {All, 0}] @ list

Using Cases

Cases[_ == a_ :> a] @ list

All produce

{0, 1, 2, 3}

$\endgroup$

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.