# Duplicate each element sublist

I have a list of the form {{0,0},{0,1},{1,0},{1,1}} and I want to duplicate each of its elements, i.e. {{0,0,0,0},{0,0,1,1},{1,1,0,0},{1,1,1,1}}. I saw some posts about similar issues, but none of them work for me, since they duplicate each "full" element of the list (e.g. {0,1} element becomes {0,1,0,1} instead of {0,0,1,1}).

For the record, I generate these lists as Tuples[Range[cmax] - 1, n], where cmax is the local dimension (two, the 0 and the 1, for the example I gave) and n is the number of elements (2 for the example).

This is a related question I've found: How to repeat each element in a list and the whole list as well?

Here are a few ways:

list = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};

Replace[list, x_ :> Splice[{x, x}], {2}]
Replace[list, x_ :> Sequence[x, x], {2}]
Join @@@ MapThread[List, {list, list}, 2]
Riffle[#, #] & /@ list
(* {{0, 0, 0, 0}, {0, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 1, 1}} *)

• Last three work perfectly, thanks! The first two output the following message: Splice::string: String expected at position 1 in Splice[{0,0}] Jun 25, 2021 at 8:59
• What version are you using? Splice will only work like this since version 12.1 Jun 25, 2021 at 9:01
• @AlbaCL I have added two more versions as alternatives to the Splice versions in older versions. (I think I like them more even than the versions using the "fancier" functionality) Jun 25, 2021 at 9:03
list = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};

k = 2;
Flatten[ConstantArray[#, k], {{2}, {3, 1}}] & @ list

{{0, 0, 0, 0}, {0, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 1, 1}}

k = 3;
Flatten[ConstantArray[#, k], {{2}, {3, 1}}] & @ list

{{0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 1, 1}, {1, 1, 1, 0, 0, 0}, {1, 1, 1, 1, 1, 1}}


Another way using the Dot product:

list.{{1,1,0,0},{0,0,1,1}}

(*  {{0, 0, 0, 0}, {0, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 1, 1}} *)

list3.{{1,1,0,0},{0,0,1,1}}

(*  {{a, a, b, b}, {c, c, d, d}, {e, e, f, f}, {g, g, h, h}}  *)


Inner[ConstantArray, list3, {2,2},Join]

(*  {{a, a, b, b}, {c, c, d, d}, {e, e, f, f}, {g, g, h, h}}  *)

Inner[ConstantArray, list3, {3,3},Join]

(*  {{a, a, a, b, b, b}, {c, c, c, d, d, d}, {e, e, e, f, f, f}, {g, g, g, h, h, h}}  *)

Inner[ConstantArray, list, {2,2},Join]

(* {{0, 0, 0, 0}, {0, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 1, 1}}  *)


Or:

Inner[Times, list3, {1,1}, {#1,#1,#2,#2}&]

(*  {{a, a, b, b}, {c, c, d, d}, {e, e, f, f}, {g, g, h, h}}  *)

Inner[Times, list, {1,1}, {#1,#1,#2,#2}&]

(*  {{0, 0, 0, 0}, {0, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 1, 1}}  *)


where

list = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};

list3={{a,b},{c,d},{e,f},{g,h}}


Edit

Use Apply (at level 1):

{#1,#1,#2,#2}&@@@list3

(*  {{a, a, b, b}, {c, c, d, d}, {e, e, f, f}, {g, g, h, h}}  *)

• And, of course, Cases[list, {x_,y_} :> {x,x,y,y}] Jun 26, 2021 at 14:14
• In addition, ArrayReduce[Flatten[{#,#},{{2,1}}]&,list3,2] Jun 27, 2021 at 7:15
• Apply, slightly shorter: {#1,##,#2}&@@@list Jun 28, 2021 at 17:31
Function[x,Delete[#, 0] & /@ ({#, #} & /@ x)] /@ YourList


Let's have try :

In Function[x,Delete[#, 0] & /@ ({#, #} & /@ x)] /@ {{0, 0}, {0, 1}, {1, 0}, {1, 1}}

Out {{0, 0, 0, 0}, {0, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 1, 1}}


Done!

• (+1) Also Map[Splice@{#,#}&, lst, {2}] Jun 30, 2021 at 13:54