Normally we recommend collecting values in a list, and making an array once, at the end. The new thing here is we need to iterate on the keys of a dictionary to do this collection.
For example:
A function to make the individual dictionaries:
In [804]: def foo(i):
...: return {k:np.arange(5) for k in ['A','B','C']}
...:
In [805]: foo(0)
Out[805]:
{'A': array([0, 1, 2, 3, 4]),
'B': array([0, 1, 2, 3, 4]),
'C': array([0, 1, 2, 3, 4])}
A collector dictionary:
In [806]: dd = {k:[] for k in ['A','B','C']}
Iteration, collecting arrays in the lists:
In [807]: for _ in range(3):
...: x = foo(None)
...: for k,v in dd.items():
...: v.append(x[k])
...:
In [808]: dd
Out[808]:
{'A': [array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4])],
'B': [array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4])],
'C': [array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4])]}
Another iteration on the dictionary to turn the lists into some sort of array (stack
, concatenate
, your choice):
In [810]: for k,v in dd.items():
...: dd[k]=np.stack(v,axis=0)
...:
In [811]: dd
Out[811]:
{'A': array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]]), 'B': array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]]), 'C': array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])}
A list of 3000 arrays of length 10 will take up somewhat more memory than one array of 30,000 numbers, but not drastically more.
You could collect the whole dictionaries in one list the first time around, but you still need to combine those into on dictionary like this.