# How to replace a comma “,” with “||” in a list

I have a list of equations:

dene = {x1== 0.2, x2== 0.4, x3==0.5};


I would like to convert this list into a rule:

iki = {ToRules[x1==0.2 || x2==0.4 || x3==0.5]} // Flatten


which works. Since my list of equations is a very long list, I'd like to use some Mathematica function.

I have tried two options to make "dene" a rule: either eeplace "==" with "->" or substitute "," with "||". I could not do any of these options work, although I've spent quite some time on the problem. I need your help.

• {ToRules[Or @@ {x1 == 0.2, x2 == 0.4, x3 == 0.5}]}? – J. M. is away Sep 28 '18 at 13:20
• Rule @@@ {x1 == 0.2, x2 == 0.4, x3 == 0.5} ? – AccidentalFourierTransform Sep 28 '18 at 13:21
• Both answers work for me, though @Accidenta's answer is more compact. – Tugrul Temel Sep 28 '18 at 15:27
• I think it's good to point out here that replacing comma's in expressions is not possible in Mathematica. The comma is a delimiter in expressions and does not represent an infix notation for a system symbol (unlike, e.g., ; which is short for CompoundExpression). You can play games with pretty much everything else, but comma's are basically untouchable unless you start converting expressions back and forth between strings (which is generally not how you want to program). Instead, you always want to find out what heads you need to transform. And if you want to splice things, use Sequence. – Sjoerd Smit Sep 28 '18 at 16:22
• This is the second line in an MMA notebook which contains the buttons File, Edit, Insert, Format, Cell, Graphics, Evaluation, Palettes, and Help. Don't hesitate to ask for further explanationin need. – user64494 Sep 29 '18 at 17:51

Much like @AccidentalFourierTransform's comment and @Pillsy's answer, you can also replace the heads but on all levels.

{x1 == 0.2, x2 == 0.4, x3 == 0.5} /. Equal -> Rule


{x1 -> 0.2, x2 -> 0.4, x3 -> 0.5}

A multi-level example

Array[x[##] == {##} &, {2, 2}]
% /. Equal -> Rule


{{x[1, 1] == {1, 1}, x[1, 2] == {1, 2}}, {x[2, 1] == {2, 1}, x[2, 2] == {2, 2}}}

{{x[1, 1] -> {1, 1}, x[1, 2] -> {1, 2}}, {x[2, 1] -> {2, 1}, x[2, 2] -> {2, 2}}}

Here is a simple function that does what I think you want.

makeRules[p : {_Equal ..}] := Rule @@@ p


Then

dene = {x1 == 0.2, x2 == 0.4, x3 == 0.5};
makeRules[dene]

{x1 -> 0.2, x2 -> 0.4, x3 -> 0.5}

• All the answers work for me, your as well. For my purpose, a simple replacement rule was needed. Your answer is much more advanced than I need but a nice structural solution. – Tugrul Temel Sep 28 '18 at 15:48
• @Tebernus. In practice, it will often be simpler just to write Rule @@@ ... but you said in your question that you would like a function. – m_goldberg Sep 28 '18 at 15:55
• Yes, in my text I used a wrong term "function", with which I meant any solution in layman's language. Since I am not a programmer, I possibly misinformed experts in this area. Sorry for that. Regards, Tebernus – Tugrul Temel Sep 28 '18 at 17:19

This is a good use for Cases with a level specification, which allows you to use rules and pattern matching to create rules! The level spec helps if you have deeper nesting of Or expressions:

expr = x1 == 0.2 || x2 == 0.4 || x3 == 0.5;

Cases[expr, lhs_ == rhs_ :> {lhs -> rhs}, Infinity]
(* {{x1 -> 0.2}, {x2 -> 0.4}, {x3 -> 0.5}} *)

• This is a good answer for pattern recognition. – Tugrul Temel Sep 28 '18 at 15:30