0

What is the best way to store and reference static information that is referred to frequently? Currently, I have a dictionary where each key is associated with a list of lists. For example,

dict = {1:[[1,2,3], [1,2,3,4,5], [1,2,3,4,5,6]],
        2:[[2,3,4,5], [1,2,3,4,5,6], [1,2,3,4,5,6]]}

The object of interest is an individual sublist like dict[1][1] which in this example has value [1,2,3,4,5]. These dictionaries will have some redundant sublist entries so that for example, [1,2,3,4,5,6] might show up as the second and third element of a superlist many times.

I will need to extend this to a third level, either through nesting the lists one level further or more likely, making the keys 3-tuples. I might even want to turn the object of interest into a pair of lists so that dict[1][1] might have value ([1,2,3,4,5],['a','b','c']).

My question is, would it make more sense to use an sqlite table rather than a dictionary? Or maybe even some completely different storage format: for example, is there a data format where different keys can point to the same value?

zoof
  • 159
  • 8

2 Answers2

1

Using dictionaries for this is fine. SQLlite would be fine as well. You could also serialize to a JSON object or use a NOSQL option like REDIS. A lot of options for whats "best" all of which have pros and cons.

Also, different dictionary keys can point to the same instance of an object you can find an example of this here How do I make a dictionary with multiple keys to one value?

Z. Fralish
  • 438
  • 4
  • 9
1

If those redundant sublists don't need to be different objects (i.e., they will always remain the same) you could just point to them to reduce memory usage. As in:

a = [1, 2, 3]
b = [1,2,3,4,5,6,7]
my_dictionary = {1: [a, b, a, a], 2: [b, b, a]}

As far as using a database, the trade off is not holding large amounts of data in memory; so a dictionary should be fine, especially if you don't need this data permanently (even if you do, a JSON dump could be useful).