3
$\begingroup$

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?

$\endgroup$

4 Answers 4

8
$\begingroup$

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}]
MapThread[Splice@*List, {list, list}, 2]
MapThread[Sequence, {list, list}, 2]
Join @@@ MapThread[List, {list, list}, 2]
MapThread[Riffle, {list, list}]
Riffle[#, #] & /@ list
(* {{0, 0, 0, 0}, {0, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 1, 1}} *)
$\endgroup$
3
  • $\begingroup$ Last three work perfectly, thanks! The first two output the following message: Splice::string: String expected at position 1 in Splice[{0,0}] $\endgroup$
    – AlbaCL
    Jun 25, 2021 at 8:59
  • $\begingroup$ What version are you using? Splice will only work like this since version 12.1 $\endgroup$
    – Lukas Lang
    Jun 25, 2021 at 9:01
  • 1
    $\begingroup$ @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) $\endgroup$
    – Lukas Lang
    Jun 25, 2021 at 9:03
3
$\begingroup$
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}}
$\endgroup$
3
$\begingroup$

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}}  *) 

In addition, using Inner:

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}}  *)
$\endgroup$
3
  • $\begingroup$ And, of course, Cases[list, {x_,y_} :> {x,x,y,y}] $\endgroup$
    – user1066
    Jun 26, 2021 at 14:14
  • $\begingroup$ In addition, ArrayReduce[Flatten[{#,#},{{2,1}}]&,list3,2] $\endgroup$
    – user1066
    Jun 27, 2021 at 7:15
  • $\begingroup$ Apply, slightly shorter: {#1,##,#2}&@@@list $\endgroup$
    – user1066
    Jun 28, 2021 at 17:31
2
$\begingroup$
Function[x,Delete[#, 0] & /@ ({#, #} & /@ x)] /@ YourList

Let's have try :

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

Out[1] {{0, 0, 0, 0}, {0, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 1, 1}}

Done!

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.