2
$\begingroup$

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[[1]],", ",b[[2]]]

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?

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

4 Answers 4

1
$\begingroup$

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

$\endgroup$
3
$\begingroup$

Converting comment into answer as suggested:

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

(* Out: 1, 2, 3 *) 
$\endgroup$
3
$\begingroup$

Also

Row[Flatten @ {a, b}, ","]

enter image description here

$\endgroup$
2
  • $\begingroup$ Wow! (+1) Even more efficient. $\endgroup$
    – Carl
    Commented Jul 6, 2018 at 20:22
  • $\begingroup$ This one is good because it does not appear to be a string. $\endgroup$
    – Carl
    Commented Jun 17 at 0:34
1
$\begingroup$

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!

$\endgroup$
4
  • 3
    $\begingroup$ Nice use of StringRiffle (+1)! Note, however, that a was not a list in the original question. $\endgroup$
    – MarcoB
    Commented Jul 6, 2018 at 17:40
  • 3
    $\begingroup$ 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[2]] gives "1.41421") $\endgroup$ Commented Jul 6, 2018 at 17:43
  • $\begingroup$ @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. $\endgroup$
    – Carl
    Commented Jul 6, 2018 at 17:46
  • $\begingroup$ 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)! $\endgroup$
    – enano9314
    Commented Jul 6, 2018 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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