8
$\begingroup$

If I have the result

{{Position, {Code}}, 
 {1, {0000, 0001}}, 
 {2, {0100, 0011}}, 
 {3, {0110, 0111}}, 
 {4, {1000, 1001}}, 
 {5, {1100,1011}}, 
 {6, {1110, 1111}}} 

and I want it to be

{{Position, Code}, 
 {1, 0000, 0001}, 
 {2, 0100, 0011}, 
 {3, 0110, 0111}, 
 {4, 1000, 1001},
 {5, 1100, 1011}, 
 {6, 1110, 1111}} 

I tried

//.{x_,{y_}}:> {x,y}

but that seems to work only for the first element.

Any help would be appreciated Thanks.

New contributor
GlomGive is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$
10
$\begingroup$
list // Map[Flatten]

{{Position, Code}, {1, 0, 1}, {2, 100, 11}, {3, 110, 111}, {4, 1000, 1001}, {5, 1100, 1011}, {6, 1110, 1111}}

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ That's an original alternative to the – more common, I presume – Flatten /@. $\endgroup$ – corey979 yesterday
6
$\begingroup$

Try

list={{Position,{Code}},{1,{0000,0001}},{2,{0100,0011}},{3,{0110,0111}},{4,{1000,1001}},{5,{1100,1011}},{6,{1110,1111}}} 

list /. {a_ ,  b_List } -> Join[{a}, b]
| improve this answer | |
$\endgroup$
6
$\begingroup$

You were really close. You just need to add another underscore to y_ as

list = {
 {Position, {Code}}, 
 {1, {0000, 0001}}, 
 {2, {0100, 0011}}, 
 {3, {0110, 0111}}, 
 {4, {1000, 1001}}, 
 {5, {1100,1011}}, 
 {6, {1110, 1111}}
};

list /. {x_, {y__}} :> {x, y}

{{Position, Code}, {1, 0000, 0001}, {2, 0100, 0011}, {3, 0110, 0111}, {4, 1000, 1001}, {5, 1100, 1011}, {6, 1110, 1111}}

| improve this answer | |
$\endgroup$
5
$\begingroup$

ReplacePart can do it.

data = 
  {{Position, {Code}}, 
   {1, {0000, 0001}}, {2, {0100, 0011}}, {3, {0110, 0111}}, 
   {4, {1000, 1001}}, {5, {1100, 1011}}, {6, {1110, 1111}}};
ReplacePart[data, {i_, 2} :> Sequence @@ data[[i, 2]]]
{{Position, Code},
 {1, 0, 1}, {2, 100, 11}, {3, 110, 111}, 
 {4, 1000, 1001}, {5, 1100, 1011}, {6, 1110, 1111}}
| improve this answer | |
$\endgroup$
4
$\begingroup$

A few additional methods:

Level[#, {-1}] & /@ list

Apply[Sequence, list, {-2}]

Map[Splice, list, {-2}]

Join[{#}, #2] & @@@ list

Prepend[#2, #]& @@@ list

MapAt[Splice, {All, 2}] @ list

ReplacePart[{_, 2, 0} -> Sequence] @ list

Map[Map @ Apply @ Sequence] @ list

Delete[{2, 0}] /@ list

FlattenAt[#, 2] & /@ list

... and for the Halloween:

enter image description here

☺ = ## & @@@ # & /@ # &;

☺ @ list
{{Position, Code}, {1, 0, 1}, {2, 100, 11}, {3, 110, 111}, {4, 1000, 1001}, 
{5, 1100, 1011}, {6, 1110, 1111}}

enter image description here

☺☺ = {#, ## & @@ #2} & @@@ # &;

☺☺ @ list
{{Position, Code}, {1, 0, 1}, {2, 100, 11}, {3, 110, 111}, {4, 1000, 1001}, 
{5, 1100, 1011}, {6, 1110, 1111}}
| improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ A little explanation: ## & @@@ # & nearly == Flatten , a nice trick ;) . So the first 'riddle' == Flatten /@ list. $\endgroup$ – wuyudi yesterday

Your Answer

GlomGive is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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