# What is better coding for turning list into sequence without curly brackets?

There are less satisfying Q/A for what I am asking but none that actually answer the question. Suppose one has

a=1; b={2,3};


And one wants to print a sequence of numbers a, b, c without curly brackets. Then if one prints

Print[a,", ",b]


One obtains

1, {2,3}

One solution to this is to print

Print[a,", ",b[],", ",b[]]


Then one obtains

1, 2, 3

What is going on is that the only way to get rid of the curly brackets in a list is to extract single values from that list. So that single value extraction by parts is a solution to my question but not an efficient one for a longer list like

 b={2,3,4,5,6,7,8,9,10,11,12,13,14}


So now the question. Is their a more efficient way to print a longer list without the curly brackets?

• Print @@ Riffle[Prepend[b, a], ", "] or Print[Fold[# <> ", " <> ToString[#2] &, ToString[a], b]]? – JungHwan Min Jul 6 '18 at 17:05
• @JungHwanMin Riffle works fine, thanks. – Carl Jul 6 '18 at 17:24
• @MarcoB Yes, also works fine. – Carl Jul 6 '18 at 17:59
• @JungHwanMin Would you write Print @@ Riffle[Prepend[b, a], ", "] as an answer, please? – Carl Jul 6 '18 at 18:06
• @MarcoB Write as an answer, please. – Carl Jul 6 '18 at 18:06

Converting a comment into an answer

One way would be

Print @@ Riffle[Prepend[b, a], ", "]


For example,

a = 1;
b = {2, 3};
Print @@ Riffle[Prepend[b, a], ", "]


1, 2, 3

Converting comment into answer as suggested:

a = 1; b = {2, 3};
Print[a, ", ", Sequence @@ Riffle[b, ", "]]

(* Out: 1, 2, 3 *)


Also

Row[Flatten @ {a, b}, ","] • Wow! (+1) Even more efficient. – Carl Jul 6 '18 at 20:22

In this case you are printing the InputForm of whatever expression you give to Print. So if you want to print the String "1, 2, 3" then you will need to construct that String to print.

The following does what you need (and works for any number of Lists of elements):

a = {1};
b = {2, 3};

str = StringRiffle[Join[a, b], ", "]


Yields

1, 2, 3


And if you want to Print this (which may not be the right tool in the first place depending on what you want to do), then you can just Print[ str ] and it will do what you want.

The idea is to join the lists together and then separate them by commas as a String.

Hope this helps!

• Nice use of StringRiffle (+1)! Note, however, that a was not a list in the original question. – MarcoB Jul 6 '18 at 17:40
• A worthy thing to note is that StringRiffle uses TextString to convert objects to Strings, so there may be unexpected conversions (e.g. TextString[Sqrt] gives "1.41421") – JungHwan Min Jul 6 '18 at 17:43
• @enano9314 Actually, Print @@ Riffle[Prepend[b, a], ", "] works better because the output is numeric or string, whereas Print[StringRiffle[Join[a, b], ", "]] interprets the numbers as strings and actually modifies the output type. For example, Anderson-Darling, 0.991363 becomes Anderson-Darling, 0.99136287 using StringRiffle. In the first case, 0.991363 still means 0.991362865780819 but is a string in the second case. – Carl Jul 6 '18 at 17:46
• Nice catch @MarcoB and @Carl these are certainly good points, and I should have been more careful. If we really just want this Printed and not as an actual string, then yes, the Riffle/Prepend` method is better here. Nice catch(es)! – enano9314 Jul 6 '18 at 19:29