# Manipulation of nested list

For example, I have {{a,b},{c,d},{e,f}} and I want {{a,b,a+b},{c,d,c+d},{e,f,e+f}}. I'm not finding a way to accomplish this.

lst = {{a, b}, {c, d}, {e, f}}
{##, +##} & @@@ lst


{{a, b, a + b}, {c, d, c + d}, {e, f, e + f}}

{##, +##} & @@@ {{a, b}, {x, y, z}, {r, s, t, u}, {w}}


{{a, b, a + b}, {x, y, z, x + y + z}, {r, s, t, u, r + s + t + u}, {w, w}}

• Very elegant... and generalizes to sublists of arbitrary length (+1). – David G. Stork Feb 16 '18 at 1:12
• Thank you @David. Good point re generalization. – kglr Feb 16 '18 at 1:44
• Delicious! (plus filler to get to 15) . – Christopher Lamb Feb 16 '18 at 4:01
Replace[list, {a_, b_} -> {a, b, a + b}, 2]


For example, you can map a function onto each element, like this:

Append[#, Total[#]] & /@ {{a, b}, {c, d}, {e, f}}
(* {{a, b, a + b}, {c, d, c + d}, {e, f, e + f}} *)


Here, the pure function Append[#, Total[#]] & is applied to each sub-list in the list {{a, b}, {c, d}, {e, f}}.

{#[[1]], #[[2]], #[[1]] + #[[2]]} & /@ {{a, b}, {c, d}, {e, f}}

{{a, b, a + b}, {c, d, c + d}, {e, f, e + f}}

• Or this {#1, #2, #1 + #2} & @@@ {{a, b}, {c, d}, {e, f}} – OkkesDulgerci Feb 16 '18 at 1:38
• Or Flatten[{#, Total@#}] & /@ lst – user1066 Feb 16 '18 at 10:56