# Output of a Print in a list

I'just starting with Mathematica and therefore I was having a small question.

It must be somewhere on Internet but I can't find it.

My question is the following.

I would like to get my values after having computed a Print[...] in a list.

I wanted to print the prime numbers <5

For[i=1,i<5,i++,
Print[Prime[i]];
]

Out:
2
3
5
7


But I would like to get these 4 values such as: {2,3,5,7} How do I compute that? I already tried Print[List[Prime[i]] but that gave me {2} {3} {5} {7}. Thank you!

And if I want to Print the prime values below a certain upperbound, let's say 10.

For[i=1,Prime[i]<10,i++,
Print[Prime[i]; ]

Out:
2
3
5
7


And I would like to get these values in a list as well, how do I do that? I was thinking about:

Prime/@range@[length[List[Prime[i]<10]]]

• Prime /@ Range@5 – eldo Nov 11 '15 at 9:57
• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 Nov 11 '15 at 10:02

## 2 Answers

Jason has the correct, idiomatic way of approaching this, so I am adding this for completeness as there is a way to capture Print output. But, you should not use this unless you have strong reasons to do so. What you can do is to use a handler function to capture the Print output. Handlers are executed at specific events, e.g.

In:= InternalHandlers[]

Out= {"VetoableValueChange" -> {JLinkPrivatejlinkVetoFunction},
"ValueTrackTrigger" -> {}, "ValueChange" -> {}, "RemoveSymbol" -> {},
"NewSymbol" -> {}, "GetFileEvent" -> {},
"MessageTextFilter" -> {InternalMessageButtonHandler},
"Message" -> {}, "Wolfram.System.Print" -> {},
"SystemAssertions" -> {}, "Assertions" -> {},
"ProcessDebugTags" -> {}, "RuntimeToolsActive" -> {},
"BreakPoint" -> {}, "GetSymbol" -> {}, "SetSymbol" -> {}}


and most are not active except under specific circumstances (like debug mode). But, "Message", "MessageTextFilter", "Wolfram.System.Print" are active all the time.

To set up a handler, there are several functions: InternalHandlerBlock, InternalAddHandler, and InternalRemoveHandler. In this case, it is best to use an InternalHandlerBlock as it automatically removes the handler when you are done using it, e.g.

Block[{out = {}},
InternalHandlerBlock[
{"Wolfram.System.Print", (out = {out, #}) &},
For[i = 1, i < 5, i++, Print[Prime[i]];]
];
Flatten@out
]


A word of caution, do not use Print inside the "Wolfram.System.Print" handler as an inescapable infinite loop is entered and you have to forcibly kill the kernel.

• Huh, a downvote. Why? It is a legitimate answer. – rcollyer Nov 12 '15 at 14:38

All Print does is print the numbers in the output line, in this case it is not necessary at all.

There are a few ways of getting the list you are looking for. The most straightforward, and useful for learning how Mathematica does things, is

Table[Prime[i], {i, 1, 4, 1}]
(* {2, 3, 5, 7} *)


Once you figure out what that means, you can use shorthand to get it quicker

Prime@Range@4
(* {2, 3, 5, 7} *)


If you want to select only those primes less than a certain number, one direct way to do that is to get a list of primes that is certainly larger than that, and use Select to get only the ones you want,

Select[Prime@Range@20, (# < 10 &)]
(* {2, 3, 5, 7} *)


But the better way, in that it doesn't do more work than is needed, is to follow this post

Prime@Range@PrimePi@10
(* {2, 3, 5, 7} *)
`