1
$\begingroup$

I have made a list partition and would like to insert an Element X into the end of every partition.

Input: {1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}...{91,92,93,94,95,X}

Output:{1,2,3,4,5,X},{6,7,8,9,10,X},{11,12,13,14,15,X}...{91,92,93,94,95,X}

I have looked at the command Insert, but I don't know what to do, when there is so many lists.

I have tried something like: Insert[[#] &, "X", Last[#] &]

$\endgroup$

closed as off-topic by Öskå, dr.blochwave, user9660, MarcoB, m_goldberg Dec 7 '15 at 12:48

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Öskå, dr.blochwave, Community, MarcoB, m_goldberg
If this question can be reworded to fit the rules in the help center, please edit the question.

8
$\begingroup$

Some more options:

ArrayPad:

ArrayPad[list, {{0, 0}, {0, 1}}, "X"]

Append in operator form:

Append["X"] /@ list

Using Function and Apply:

{##, "X"} & @@@ list

With Replace:

Replace[list, {x__} :> {x, "X"}, 1]
$\endgroup$
  • $\begingroup$ Probably the fastest of all. +1. $\endgroup$ – Rorschach Dec 7 '15 at 6:41
4
$\begingroup$

Another solution using ConstantArray and Transpose

list = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}};
Flatten /@ Transpose[{list, ConstantArray[X, Length@list]}]

{{1, 2, 3, 4, 5, X}, {6, 7, 8, 9, 10, X}, {11, 12, 13, 14, 15, X}}

$\endgroup$
4
$\begingroup$

Append would work.

list = {{7, 5, 8, 3, 4}, {4, 8, 9, 7, 3}, {7, 0, 1, 4, 6},
 {3, 0, 4, 7, 9}, {5, 1, 2, 7, 4}};
Append[#, "X"] & /@ list

(* {{7, 5, 8, 3, 4, "X"}, {4, 8, 9, 7, 3, "X"}, {7, 0, 1, 4, 6, "X"},
 {3, 0, 4, 7, 9, "X"}, {5, 1, 2, 7, 4, "X"}} *)
$\endgroup$
4
$\begingroup$
list = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}};

 Flatten /@ Thread[List[list, X]]

{{1, 2, 3, 4, 5, X}, {6, 7, 8, 9, 10, X}, {11, 12, 13, 14, 15, X}}

$\endgroup$
  • $\begingroup$ Your MapAt code overwrites the last element of each list rather than adding a new one. $\endgroup$ – Simon Woods Dec 6 '15 at 21:37
3
$\begingroup$
SeedRandom[12]
list = RandomInteger[10, {4, 5}]
Join[#, {"x"}] & /@ list

(*Out:
{{2, 4, 0, 10, 1}, {9, 7, 3, 1, 10}, {7, 0, 9, 5, 4}, {6, 7, 2, 8, 5}}
{{2, 4, 0, 10, 1, "x"}, {9, 7, 3, 1, 10, "x"}, {7, 0, 9, 5, 4, "x"}, {6, 7, 2, 8, 5, "x"}}
*)
$\endgroup$
  • $\begingroup$ Very slow for very large lists. $\endgroup$ – Rorschach Dec 7 '15 at 6:42
3
$\begingroup$

Using Insert you can do as follows

list = {{4, 7, 8, 8, 5}, {7, 2, 9, 7, 0}, {2, 7, 1, 6, 5}, {6, 6, 7, 
4, 3}};

Insert[#, x, Length@# + 1] & /@ list

(*{{4, 7, 8, 8, 5, x}, {7, 2, 9, 7, 0, x}, {2, 7, 1, 6, 5, x}, {6, 6, 7,
 4, 3, x}}*)
$\endgroup$
  • $\begingroup$ Very slow for very large lists. $\endgroup$ – Rorschach Dec 7 '15 at 6:41
3
$\begingroup$

If lists form a regular array (good for packed arrays):

lists=Partition[Range[15], 5];
PadRight[lists, {Automatic, 1 + Length@First@lists}, x]
(*  {{1, 2, 3, 4, 5, x}, {6, 7, 8, 9, 10, x}, {11, 12, 13, 14, 15, x}}  *)

Or:

Append[#, "X"] & /@ lists
$\endgroup$
0
$\begingroup$

A simple case of patterns,

Partition[Range[20], 4] //. {s___Integer} :> {s, "x"}

{{1, 2, 3, 4, "x"}, {5, 6, 7, 8, "x"}, {9, 10, 11, 12, "x"}, {13, 14, 
  15, 16, "x"}, {17, 18, 19, 20, "x"}}
$\endgroup$

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