3 added 195 characters in body edited Jun 11 '14 at 2:07 ciao 17.7k11 gold badge3838 silver badges109109 bronze badges a = {{1.1, 2.2}, {1.1, 2.2}, {x, b}, {c, d}, {x, b}, {1, 2}} # /. (Rule[First@#, Append @@ #] & /@ Tally[#]) &[a] (* {{1.1, 2.2, 2}, {1.1, 2.2, 2}, {x, b, 2}, {c, d, 1}, {x, b, 2}, {1, 2,1}} *)  For huge lists: # /. Dispatch[Rule[First@#, Append @@ #] & /@ Tally[#]] &[a];  Will speed things up nicely. Also quite fast on large lists: Partition[Flatten[Riffle[#, Join @@ ConstantArray @@@ Tally[#][[All, {2, 2}]]]], 3] &@a  Unlike the above (where order of elements does not matter), this takes advantage of the OP assumptions of same elements appearing in sequence. Even faster on large arrays: Block[{tmp = ArrayPad[#, {{0, 0}, {0, 1}}], t = Tally[#]}, tmp[[All, 3]] = Join @@ ConstantArray @@@ t[[All, {2, 2}]]; tmp] &[data]  a = {{1.1, 2.2}, {1.1, 2.2}, {x, b}, {c, d}, {x, b}, {1, 2}} # /. (Rule[First@#, Append @@ #] & /@ Tally[#]) &[a] (* {{1.1, 2.2, 2}, {1.1, 2.2, 2}, {x, b, 2}, {c, d, 1}, {x, b, 2}, {1, 2,1}} *)  For huge lists: # /. Dispatch[Rule[First@#, Append @@ #] & /@ Tally[#]] &[a];  Will speed things up nicely. Also quite fast on large lists: Partition[Flatten[Riffle[#, Join @@ ConstantArray @@@ Tally[#][[All, {2, 2}]]]], 3] &@a  Unlike the above (where order of elements does not matter), this takes advantage of the OP assumptions of same elements appearing in sequence. a = {{1.1, 2.2}, {1.1, 2.2}, {x, b}, {c, d}, {x, b}, {1, 2}} # /. (Rule[First@#, Append @@ #] & /@ Tally[#]) &[a] (* {{1.1, 2.2, 2}, {1.1, 2.2, 2}, {x, b, 2}, {c, d, 1}, {x, b, 2}, {1, 2,1}} *)  For huge lists: # /. Dispatch[Rule[First@#, Append @@ #] & /@ Tally[#]] &[a];  Will speed things up nicely. Also quite fast on large lists: Partition[Flatten[Riffle[#, Join @@ ConstantArray @@@ Tally[#][[All, {2, 2}]]]], 3] &@a  Unlike the above (where order of elements does not matter), this takes advantage of the OP assumptions of same elements appearing in sequence. Even faster on large arrays: Block[{tmp = ArrayPad[#, {{0, 0}, {0, 1}}], t = Tally[#]}, tmp[[All, 3]] = Join @@ ConstantArray @@@ t[[All, {2, 2}]]; tmp] &[data]  2 added 274 characters in body edited Jun 10 '14 at 23:38 ciao 17.7k11 gold badge3838 silver badges109109 bronze badges a = {{1.1, 2.2}, {1.1, 2.2}, {x, b}, {c, d}, {x, b}, {1, 2}} # /. (Rule[First@#, Append @@ #] & /@ Tally[#]) &[a] (* {{1.1, 2.2, 2}, {1.1, 2.2, 2}, {x, b, 2}, {c, d, 1}, {x, b, 2}, {1, 2,1}} *)  For huge lists: # /. Dispatch[Rule[First@#, Append @@ #] & /@ Tally[#]] &[a];  Will speed things up nicely. Also quite fast on large lists: Partition[Flatten[Riffle[#, Join @@ ConstantArray @@@ Tally[#][[All, {2, 2}]]]], 3] &@a  Unlike the above (where order of elements does not matter), this takes advantage of the OP assumptions of same elements appearing in sequence. a = {{1.1, 2.2}, {1.1, 2.2}, {x, b}, {c, d}, {x, b}, {1, 2}} # /. (Rule[First@#, Append @@ #] & /@ Tally[#]) &[a] (* {{1.1, 2.2, 2}, {1.1, 2.2, 2}, {x, b, 2}, {c, d, 1}, {x, b, 2}, {1, 2,1}} *)  For huge lists: # /. Dispatch[Rule[First@#, Append @@ #] & /@ Tally[#]] &[a];  Will speed things up nicely. a = {{1.1, 2.2}, {1.1, 2.2}, {x, b}, {c, d}, {x, b}, {1, 2}} # /. (Rule[First@#, Append @@ #] & /@ Tally[#]) &[a] (* {{1.1, 2.2, 2}, {1.1, 2.2, 2}, {x, b, 2}, {c, d, 1}, {x, b, 2}, {1, 2,1}} *)  For huge lists: # /. Dispatch[Rule[First@#, Append @@ #] & /@ Tally[#]] &[a];  Will speed things up nicely. Also quite fast on large lists: Partition[Flatten[Riffle[#, Join @@ ConstantArray @@@ Tally[#][[All, {2, 2}]]]], 3] &@a  Unlike the above (where order of elements does not matter), this takes advantage of the OP assumptions of same elements appearing in sequence. 1 answered Jun 10 '14 at 22:27 ciao 17.7k11 gold badge3838 silver badges109109 bronze badges a = {{1.1, 2.2}, {1.1, 2.2}, {x, b}, {c, d}, {x, b}, {1, 2}} # /. (Rule[First@#, Append @@ #] & /@ Tally[#]) &[a] (* {{1.1, 2.2, 2}, {1.1, 2.2, 2}, {x, b, 2}, {c, d, 1}, {x, b, 2}, {1, 2,1}} *)  For huge lists: # /. Dispatch[Rule[First@#, Append @@ #] & /@ Tally[#]] &[a];  Will speed things up nicely.