5
$\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$

9 Answers 9

9
$\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
4
$\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$
4
$\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
3
$\begingroup$
list = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};

Using Cases

Cases[list, {a_, b_} -> {a, a, b, b}]

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

$\endgroup$
3
$\begingroup$
list = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};

Using ReplaceList:

ReplaceList[list, {___, s : {a_, b_}, ___} :> Join @@ Thread@{s, s}]

(*{{0, 0, 0, 0}, {0, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 1, 1}}*)
$\endgroup$
3
$\begingroup$

Also using ArrayPad

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

ArrayPad[#, 1, "Fixed"] & /@ list

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

$\endgroup$
3
$\begingroup$

You can use SubstitutionSystem

For example:

rules = {1 -> {1, 1}, 0 -> {0, 0}};
v = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
Join @@@ SubstitutionSystem[rules, v, 1][[2]]

yields:

{{0, 0, 0, 0}, {0, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 1, 1}}
$\endgroup$
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
  • 1
    $\begingroup$ (+1) Also Map[Splice@{#,#}&, lst, {2}] $\endgroup$
    – user1066
    Jun 30, 2021 at 13:54
1
$\begingroup$
la = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};

lb = {{0, 0, 1}, {0, 1, 1}, {1, 0, 1}, {1, 1, 1}};

Using SequenceCases

SequenceCases[la, {x_} :> Flatten[{x, x}]]

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

SequenceCases[lb, {x_} :> Flatten[{x, x}]]

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

$\endgroup$

Your Answer

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

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