How to export data from For loop in text file?

I'm new in Mathematica and I am trying to write numbers from 1 to 10 in txt file. But "Null" is everything that is written in my file. The code is:

Export["C:\\Users\\Sealy\\Desktop\\list.txt",
For[k = 1, k <= 10, k++, Print[List[k]]]]
• Have you seen this? Sep 4 '12 at 15:06
• @stevenvh It's the name that halted me. =:-O Sep 15 '12 at 14:04

Here are some variants to achieve what you want using various notations and avoiding the use of the dreaded For loop.

Exporting as strings

Standard notation:

Export[ "/tmp/10.txt", Range@10]

Infix notation:

"/tmp/10.txt"~Export~Range@10

Exporting in Mathematica format

Save as a list, so it can easily be read back into mathematica using Get.

Range@10 >> "/tmp/10.mma"

Read it back in using the short form of the function Get:

<< "/tmp/10.mma"

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Why you might not want a For loop

For loops aren't used very much in Mathematica as there are more efficient ways to approach the need to repeat computations. Lists are fundamental to what Mathematica does and in conjunction with other techniques such as Map make For loops and Do,While redundant.

Map

The Map function, shortform /@, which can apply a function to a list of items.

Sin/@{1,2,3,4,5,6} - > {Sin, Sin, Sin, Sin, Sin, Sin}

Listability

The ability of functions to apply themselves to a list,

Sin[{1,2,3,4,5,6}] -> {Sin, Sin, Sin, Sin, Sin, Sin}

Functions can Return Lists

( Here we use the short form of function invocation, f@arg instead of f[arg] )

Range@6 -> {1,2,3,4,5,6}

and

Sin@Range@6-> {Sin, Sin, Sin, Sin, Sin, Sin}

Recommendation: Read up on Table, Range etc. . Press F1 often!

Export[NotebookDirectory[] <> "test.txt", Range]

Print is used for it's side effect (which is printing to the screen) but it returns Null. Also the For loop returns Null

Try this:

lst = {};
For[k = 1, k <= 10, k++, AppendTo[lst, {k}]]
lst
(* {{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}} *)
Export["C:\Users\Sealy\Desktop\list.txt", lst]
• I have managed to export lists and to import them in Mathematica using ways you all suggested. but now, I have to sum two elements of those imported lists (for example, for {d_1, d_2, d_3} I have to find sums d_1+d_2, d_2+d_3 and d_1+d_3 and to deal with those sums that satisfy some conditions). How can I do that? I saw a lot of examples that use "#" as a variable of a list but I think I cannot use that two times (For example, If[#+#], Print does not make sense)? Thank you very much! Sep 4 '12 at 10:20
• @WayneGacy, that's a new question. Function[in, in + RotateLeft[in]][{d1, d2, d3}]
– user21
Sep 4 '12 at 10:24
• Ups, thank you! Sep 4 '12 at 10:27

Try

(* Initiate an empty List *)
list = {};
(* Use AppendTo in order to add  the entries in each iteration *)
For[k = 1, k <= 10, k++, list = AppendTo[list, k]];
(* Then export the List *)
Export["C:\Users\Sealy\Desktop\list.txt", list]

However the simplest Mathematica way to do it will be by calling Table

list=Table[k,{k,1,10}];
Export["C:\Users\Sealy\Desktop\list.txt", list]

BR

• Table[k,{k,1,10}] is waaay too long. Range does exactly the same. Sep 4 '12 at 10:35
• @stevenvh this was meant to introduce the very commonplace and still powerful MMA function Table to the OP as he claims to be a novice. I thought of explainign him a bit more in detail but image_dioctor has already done that... Sep 4 '12 at 11:27