Message Boards Message Boards

[✓] Concatenate elements in a list?

GROUPS:

I'm very new to Mathematica, so here comes a newbie question.

I have a list of numbers, such as {12,35}, and want to concatenate them to produce a single-element list: {1235} in this example. I then want to extract that element as an integer, 1235. Can someone please give me a clue.

Geoffrey Marnell

POSTED BY: geoffrey Marnell
Answer
17 days ago

You can turn a number into a list of its digits using IntegerDigits. If you do that on all your numbers, you have a list where each element is a list of digits. Then you can Flatten this list to get one long list of digits. After that, you rebuild a new Integer.

FromDigits[Flatten[IntegerDigits[{12, 35}]]]

(* 1235 *)
POSTED BY: Patrick Scheibe
Answer
17 days ago

Beat me to it!

IntegerDigits will break up the numbers, into sub-lists

IntegerDigits@{12, 35}

{{1, 2}, {3, 5}}

Flatten will break up the sub-lists

Flatten@IntegerDigits@{12, 35}

{1, 2, 3, 5}

FromDigits will make a new integer from the flattened list

FromDigits@Flatten@IntegerDigits@{12, 35}

1235

RightComposition allows the operations to be read from left to right

{12, 35} // RightComposition[IntegerDigits, Flatten, FromDigits]

1235

Another way of saying RightComposition is /*

RightComposition[
 IntegerDigits,
 Flatten,
 FromDigits]

IntegerDigits /* Flatten /* FromDigits

Make a compound operation called joinDigits, or as you like

joinDigits = IntegerDigits /* Flatten /* FromDigits;

Call the new operation on your list

joinDigits[{12, 35}]

1235

joinDigits[{12, 35, 69}]

123569

POSTED BY: Christopher French
Answer
17 days ago

Thanks Christopher, thanks Patrick. Geoffrey Marnell

POSTED BY: geoffrey Marnell
Answer
9 days ago

Group Abstract Group Abstract