# Rearrangement of the list

I have the following list:

 m = {{x == 0, y == 0.29264681456942615},
{x == 30, y == 0.2419119568894183},
{x == 50, y == 0.1485164898707659},
{x == 70, y == 0.05437093382683481},
{x == 90 , y == 1.}}


I would like to convert it to the form {{0,0.29264681456942615},{30, 0.2419119568894183}, ... } (extracting only the numbers form the list). How can I do this?

• m[[;; , ;; , 2]] should do the trick. To see why check the result of the following: Head[a==b], (a==b)[[0]], (a==b)[[1]], (a==b)[[2]]
– ssch
Commented Nov 6, 2012 at 14:32
• alternatively m /. Equal[_, b_] -> b Commented Nov 6, 2012 at 14:42
• That's way neater since it can be used even if the order was {3==x,8==y} in some places, m /. Equal[a_, b_] :> If[NumberQ[a], a, b]
– ssch
Commented Nov 6, 2012 at 14:54
• Map[Last, m, {2}]. Commented Nov 6, 2012 at 14:56
• @ssch equivalent but perhaps less cryptic for the novice: m[[All,All,2]] Commented Nov 6, 2012 at 15:05

One of many possible ways is

{x, y} /. (m /. Equal -> Rule)


What happens here: Let's say you want to transform the expression x equals 0 into x is replaced by 0, then you can do exactly this by ReplaceAll (ok, replacing all) Equals in your list into Rules which is done by (m /. Equal -> Rule). The rest is to use it and replace {x,y} with these rules.

One disadvantage of this approach which is shared by following solutions from the comments:

m[[;; , ;; , 2]]
m[[All,All,2]]
m[[All, All, -1]]
m /. Equal[_, b_] -> b
Map[Last, m, {2}]


is that it relies on the fact, that your number is in the end. This can be prevented by using for instance a rule which checks where the numeric value is like suggested by ssch

m /. Equal[a_, b_] :> If[NumberQ[a], a, b]

• Hmmm... why the RuleDelayed in your first example? Rule works too. +1 for the didactic explanation. Commented Nov 6, 2012 at 15:18
• @IstvánZachar Thx. No special reason. I use RuleDelayed more often, since in most of my applications I don't want to rhs to be evaluated. But I see, it is maybe confusing the OP here. Commented Nov 6, 2012 at 15:20
• @halirutan the last rule still needs to be delayed (I couldn't edit because 1 character was considered a low quality edit by the system)
– ssch
Commented Nov 6, 2012 at 15:31
• @ssch Ah, yes, sorry. I was a bit beside me. Commented Nov 6, 2012 at 15:37
• @ssch To explain why I thought it was not necessary: something like If[boing, a, b] stays unevaluated as long as boing is not True or False. I thought we can use this here and forgot that NumberQ[a] is instantly evaluated to False. Sorry again. Commented Nov 6, 2012 at 15:40
m = {{x == 0, y == 0.29264681456942615}, {x == 30,
y == 0.2419119568894183}, {x == 50,
y == 0.1485164898707659}, {x == 70,
y == 0.05437093382683481}, {x == 90, y == 1.}};

m /. {x == a_, y == b_} -> {a, b}


This returns

{{0, 0.292647}, {30, 0.241912}, {50, 0.148516}, {70, 0.0543709}, {90, 1.}}


m = {{x == 0, y == 0.29264681456942615}, {x == 30, y == 0.2419119568894183}, {x == 50, y == 0.1485164898707659}, {x == 70, y == 0.05437093382683481}, {x == 90, y == 1.}};

If lists had this shape I would just

m[[All, All, 2]]


Alternatives are

m /. _ == n_ :> n


or

{x, y} /. (m /. Equal -> Rule)


or even

Block[{Equal = CompoundExpression}, m]


or odd

{x, y} /. First /@ Solve /@ m


all to get

(* {{0, 0.292647}, {30, 0.241912}, {50, 0.148516}, {70, 0.0543709}, {90,
1.}} *)

• Ok, I just saw that all these except the even and odd ones were already mentioned in comments or answers
– Rojo
Commented Nov 7, 2012 at 0:08
Block[{Equal = (#2 &)}, m]


or

m /. Equal -> (#2 &)


{{0, 0.292647}, {30, 0.241912}, {50, 0.148516}, {70, 0.0543709}, {90, 1.}}