I have two lists and I want to take the difference keeping the first elements of the sublists ( X1,X2
below) intact which are the same in both lists. Is there any easier way to do it without explicitly going through each list element and taking the differences except the first one? I tried
listA = {{1}, {X1, a, b}, {X2, c, d}};
listB = {{0}, {X1, a0, b0}, {X2, c0, d0}};
MapThread[{First[#1] , Rest[#1] - Rest[#2]} &, {listA, listB}]
Output:
{{1, {}}, {X1, {a - a0, b - b0}}, {X2, {c - c0, d - d0}}}
Expected output:
{{1}, {X1, a - a0, b - b0}, {X2, c - c0, d - d0}}
How do I remove these unwanted braces?
Flatten[#, 1] & /@ MapThread[{First[#1], Rest[#1] - Rest[#2]} &, {listA, listB}]
$\endgroup$Flatten
but as//Flatten
which removed all the braces which is why it did not work. $\endgroup$MapThread[{First[#1], Sequence @@ (Rest[#1] - Rest[#2])} &, {listA, listB}]
$\endgroup$MapThread[{First[#1], Splice[Rest[#1] - Rest[#2]]} &, {listA, listB}]
$\endgroup$