I am running calculations that will generate a list of lists of integers. Throughout the calculations, the number of lists will increase, and already-existing lists will be added to. What is the most efficient way to implement this for lists of arbitrary length without causing a bunch of slow list copies?
The following toy code illustrates the problem with random integers. Using AppendTo[] causes the lists to be copied over and over again. I will not know the number of lists or their lengths beforehand. The variable n will be set as high as I have enough virtual memory for.
EDIT: Just to be really clear, lists will be added and added-to constantly, after every iteration of my ongoing calculation.
n=25;
a = {{}};
Do[
j = RandomInteger[{1, Length[a]}];
AppendTo[a[[j]], RandomInteger[9]];
If[OddQ[j], AppendTo[a, {}]], {i, 1, n}];
a
Reap
andSow
algorithm. $\endgroup$Internal`Bag
as well. $\endgroup$