I'm not able to delete -> in {{x -> a}, {x -> b}, {x -> c, x -> d}}
DeleteCases
does not by default operate on heads. You can set the Heads
option to True
to change that.
DeleteCases[{{x -> a}, {x -> b}, {x -> c, x -> d}}, Rule, {3}, Heads -> True]
{{x, a}, {x, b}, {x, c, x, d}}
Note that the last term has become {x, c, x, d}
which is not quite what you expected, but it is logically consistent if we expect {x -> a}
to become {x, a}
.
A simpler path to the same output in this case is:
{{x -> a}, {x -> b}, {x -> c, x -> d}} /. Rule -> Sequence
{{x, a}, {x, b}, {x, c, x, d}}
Your expected output can be had from:
{{x -> a}, {x -> b}, {x -> c, x -> d}} /. Rule -> List /. {x_List} :> x
{{x, a}, {x, b}, {{x, c}, {x, d}}}
Recommended reading:
- How to completely delete the head of a function expression
Replacing heads is generically supported by Apply (@@ / @@@)
. So a functional way may look like this:
rules = {{x -> a}, {x -> b}, {x -> c, x -> d}};
If[Length[#] == 1, Flatten[#], Identity[#]] & /@ Apply[List, rules, {-2}]
{{x, a}, {x, b}, {{x, c}, {x, d}}}
Or
If[Length[#] == 1, Sequence @@@ #, List @@@ #] & /@ rules
returns the same result.