Importing and exporting Tables with mixed dimensions

How can I export a table which looks like this:

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

And then Import it so that the list will be the same? I tried csv and txt with “Table” and “Data” options. They just either add additional commas in wrong places or for the case of csv interprets some parts as strings.

If you want to save Mathematica symbols and variables, it might be better to save DumpSave, as in

SetDirectory[NotebookDirectory[]];
lst = {{{1, 2}, {4, 5}}, {5}, {1, 2, 3, 4}};
DumpSave["foo.mx", lst];
Clear[lst];
Get["foo.mx"];
lst
(* {{{1, 2}, {4, 5}}, {5}, {1, 2, 3, 4}} *)

You can also use “List” but you’d have to use ToExpression. So I think mx might be better

SetDirectory[NotebookDirectory[]];
lst = {{{1, 2}, {4, 5}}, {5}, {1, 2, 3, 4}};
Export["foo.txt", lst, "List"];
lst0 = ToExpression[Import["foo.txt", "List"]];
lst === lst0
(*  True  *)

Thanks. I can’t do dumpsave as I need to be able to transfer files between machines.

Simple text works

lm = {{{1, 2}, {4, 5}}, {5}, {1, 2, 3, 4}}
SetDirectory["H:\\"];
Put[lm, "test.txt"]
newlm = << "test.txt";
lm == newlm

Result is “True”

Thanks!