Search code examples
wolfram-mathematicanested-listsreplaceall

How to replace one element with multiple elements in Mathematica?


I have a nested list as follows:

bpas = {{1, 1/19, 3}, {2, 3/19, 3}, {3, 7/19, 1}}

I want to replace the element whose first part equals to 2 with more than one element. In this case it would be {2, 3/19, 3}, replaced by {2, 6/19, 3}, {1, 1/19, 6}.

Say, I want to get the following form:

bpas = {{1, 1/19, 3}, {2, 6/19, 3}, {1, 1/19, 6}, {3, 7, 7/19, 1}}

I used the syntax bpas /. {x_ /; x == 2, y_, z_} -> {{2, 2 y, z}, {1, y/3, 2 z}}, but only getting:

{{1, 1/19, 3}, {{2, 6/19, 3}, {1, 1/19, 6}}, {3, 7/19, 1}}

So, how can I get the proper output?


Solution

  • bpas /. {x_ /; x == 2, y_, z_} -> Sequence[{2, 2 y, z}, {1, y/3, 2 z}]