i recall a module that did this, but i haven't gone to look for it, yet. i think it was called collection. i want to stored keyed data which could have multiple data objects under the same key. what i would normally do is just keep everything in a list, with all lists in a dictionary under the respective key, and append if the list is already there. that way i have every object that has that key and they are in the order stored (i don't need the ordering). right now i am wondering if my simple way is good enough or if i should go find that module.
Here's the documentation for
collections. Does anything sound suitable?
Sound like defaultdict with list.
>>> from collections import defaultdict
>>>
>>> d = defaultdict(list)
>>> s = [('green', 1), ('blue', 25), ('green', 100), ('blue', 4), ('red', 1)]
>>> for k, v in s:
... d[k].append(v)
...
>>> d.items()
dict_items([('green', [1, 100]), ('blue', [25, 4]), ('red', [1])])
I never used it, but the interface seems to be very easy:
import multidict
md = multidict.MultiDict()
md['foo'] = 1337
md.add('foo', 42)
print(md.getall('foo'))
Output:
[1337, 42]
i think i was thinking about collections.Counter when i was thinking there was a way to work with multiple things with the same key.
i don't think defaultdict will help much since a list in a regular dictionary can accomplish this,
but multidict might be the thing. any documentation on multidict? i am wondering if md.add('foo',42)
depends on 'foo' already being in there ... iow, do i have to do md['foo'] the first time. or was that coded just to show it could be added either way? maybe doing 2 one way and 2 the other way would show. will there be 4 instances of 'foo' keys in md?