Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It really depends on what you want to do with the values in your inner lists/dictionaries. If, when you add a new entry, you want the inner list to have only unique values, then the list implementation will be <em>much</em> slower for large lists. It scales roughly at O(n), rather than O(1) [average case] for dictionaries.</p> <p>If you don't care about multiples in those inner lists, then it is a closer thing. </p> <p>I would use dictionaries, as you have. Python's dictionaries are <em>very</em> efficient (speaking as someone who's tried to implement dictionary data structures in C for real time applications).</p> <p>As for not using sets. It would be better (since memory isn't an issue, you say), to tweak the serialization, and have the speed critical part of your code be as simple as possible. After deserialization, just go through and convert the lists to sets:</p> <pre><code>big_dict = {"the" : [1, 2, 3, 4, 5]} # imagine we got this from JSON for key, value in big_dict: big_dict[key] = set(value) </code></pre> <p>Should do it. Unless you are serializing / deserializing the whole index all the time, this added pre-processing costs should be amortized over enough requests not to matter.</p> <p>Alternatively you can register encoders and decoders with JSON so you can do this conversion automatically. I usually don't bother when the issue is something so small and contained, however.</p> <p>So in your dictionary based approach you could do:</p> <pre><code>for key, value in smaller_dict.items(): if key in big_dict: big_dict[key].update(value) else: big_dict[key] = value </code></pre> <p>If you want the big_dict to only <em>copy</em> the dictionary, use <code>dict(value)</code> instead of <code>value</code> on the last line. You could also use <code>try:</code> and <code>except KeyError</code> in the last loop, but <code>if</code>...<code>else</code> is a fraction faster (on my machine, YMMV).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload