2
$\begingroup$

How to convert the form of a solution from solve as follows:

{x -> 1, y -> 1, z -> Sqrt[2]}

into a list form

{1, 1, Sqrt[2]}?

$\endgroup$
2
  • 2
    $\begingroup$ The canonical way is {x, y, z} /. {x -> 1, y -> 1, z -> Sqrt[2]}. $\endgroup$
    – march
    Apr 20 at 17:36
  • 1
    $\begingroup$ (-1) After asking 247 undeleted questions, you still don't learn anything about the usage of /., not to mention basic list manipulation. $\endgroup$
    – xzczd
    Apr 21 at 2:01

4 Answers 4

5
$\begingroup$
sol = {x -> 1, y -> 1, z -> Sqrt[2]};

Some other possibilities

Using Cases

Cases[(_ -> v_) :> v] @ sol

Using ReplaceAll

sol /. Rule[_, v_] :> v

Using Query

Query[All, 2] @ sol

Using Map

Map[Last] @ sol

All produce

{1, 1, Sqrt[2]}

$\endgroup$
4
$\begingroup$

Try

{x -> 1, y -> 1, z -> Sqrt[2]}[[All, 2]]

or

{x -> 1, y -> 1, z -> Sqrt[2]} // Values
$\endgroup$
3
$\begingroup$

If you used Solve to get these solutions, you can avoid the need for post-processing the result by using SolveValues instead.

$\endgroup$
2
$\begingroup$

Adding to what eldo said, I really like manual pattern matching even for very annoying cases. For your case:

sol = {x -> 1, y -> 1, z -> Sqrt[2]};
Replace[sol, {x -> a_, y -> b_, z -> c_} :> {a, b, c}]

But let's say something very annoying that needs counting levels or trial and error, I just copy the original list and paste in the second argument of Replace and replace the parts I want with variables:

annoyingsol = {{{{{x -> 1}}}}, y -> 1, z -> Sqrt[2]};
Replace[annoyingsol, {{{{{x -> a_}}}}, y -> b_, z -> c_} :> {a, b, c}]
$\endgroup$

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