Best way to implement 2-D array of series elements in Python
Question: I have a dynamic set consisting of a data series on the order of hundreds of
objects, where each series must be identified (by integer) and consists of
elements, also identified by an integer. Each element is a custom class.

I used a defaultdict to created a nested (2-D) dictionary. This enables me to
quickly access a series and individual elements by key/ID. I needed to be able
to add and delete elements and entire series, so the dict served me well. Also
note that the IDs do not have to be sequential, due to add/delete. The IDs are
important since they are unique and referenced elsewhere through my
application.

For example, consider the following data set with keys/IDs,

    
    
    [1][1,2,3,4,5]
    [2][1,4,10]
    [4][1]
    

However, now I realize I want to be able to insert elements in a series, but
the dictionary doesn't quite support it. For example, I'd like to be able to
insert a new element between 3 and 4 for series 1, causing the IDs above it
(from 4,5) to increment (to 5,6):

    
    
    [1][1,2,3,4,5] becomes
    [1][1,2,3,4(new),5,6]
    

The order matters since the elements are part of a sequential series. I
realize that this would be easier with a nested list since it supports
insert(), but then I would be forced to iterate over the entire 2-D array to
get element indices right?

What would be the most optimal way to implement this data structure in Python?


Answer: I think what you want is a dict with array values:

    
    
    dict = {1:[...],3:[...], ....}
    

You can then operate on the arrays as you please. If the array values are
sequential ints just use:

    
    
    dict[key].append(vals)
    dict[key].sort()
    

Don't worry about the speed unless you find out it's a problem. Premature
optimization is the root of all evil.

In fact, don't even sort your dict vals until you have to, if you want to be
really efficient.

