2
$\begingroup$

How do i Append(Prepend) to a List?

A = {{a,b},{c,d}} // TableForm
B = {x, y}
MapThread[Prepend, {A,B}]

does not work. If i leave out the TableForm it does. This could be solved by converting TableForm of a List back to a list which works:

A = {{a,b},{c,d}}
B = {x, y}
MapThread[Prepend, {A,B}]

But how do i do such if A allready is in TableForm? Should i generally allways avoid outputting as TableForm if the structure contains data?

$\endgroup$
2
$\begingroup$
MapAt[MapThread[Prepend, {#, B}] &, A, {1}]

enter image description here

$\endgroup$
1
$\begingroup$

First of all you should define A without //TableForm

A = {{a,b},{c,d}}  
B = {x, y}
Transpose[Join[{B},A]] (* or Thread[Join[{B}, A ]]*)
(*{{x, a, c}, {y, b, d}}*) 
$\endgroup$
  • $\begingroup$ This circumvention of the issue was allready mentioned in the question. I looked for a solution which does this on TableForms etc. I was able to solve it myself after having posted the question hence i answered myself. $\endgroup$ – Gladaed Dec 13 '18 at 16:46
  • 1
    $\begingroup$ Sorry for trying to give you a helpful answer. $\endgroup$ – Ulrich Neumann Dec 13 '18 at 16:51
  • 1
    $\begingroup$ @Gladaed You are going to make yourself very unhappy by sticking to wrapping things into TableForm. $\endgroup$ – Henrik Schumacher Dec 13 '18 at 17:39
0
$\begingroup$

I just found a solution myself:

AList = A[[1]] 

and hence

A = {{a,b},{c,d}} // TableForm
B = {x, y}
MapThread[Prepend, {A[[1]],B}] // TableForm

since ListForm is just a Head. This way most heads can be detached.

$\endgroup$
  • 3
    $\begingroup$ What I usually do in my code is add parentheses, "( A = {{a,b},{c,d}} ) // TableForm" so it keeps lists as lists, but I can see it in TableForm, or MatrixForm, or whatever. $\endgroup$ – MikeY Dec 13 '18 at 16:07

Your Answer

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.