[Python-ideas] Dict view on list of two-tuples
Steven D'Aprano
steve at pearwood.info
Tue Apr 5 00:56:24 CEST 2011
Ian Bicking wrote:
> It would be nice if the stdlib had a data structure that represented a dict
> view on a list of key/value tuples. This has come up a bit when thinking
> about common web request/response objects, as they all have an object like
> this but they all have slightly different APIs (and sometimes slightly
> different notions of what they want to do).
I don't know whether "dict view" is the right terminology, but I've
frequently found myself writing code that accepts a mapping, either a
dict or a list of (key,value) items. So far I've handled this in an ad
hoc way, only implementing as much functionality as I've needed at the
time. I can't say it's been a major burden.
Perhaps the simplest thing would be a key/value mapping (kvmap?) type
that wraps such a list with a dict interface. Which parts of the dict
interface (all of it? only some of it?) remains to be answered.
For sure though, it would have to loosen the requirement that keys are
unique and unsorted. (key,value) pairs are only useful for when you need
non-unique, ordered keys, otherwise I'd just use a dict :)
> Data structures with an internal representation like {'a':
> ['1', '3'], 'b': ['2']} are not sufficient.
How do you know? The ordered dict implementation in the standard library
has an internal dict representation (or at least it did, when it was on
ActiveState). Why do you care whether the implementation uses a dict
internally, so long as the interface matches the specified (and as yet
not entirely decided) API?
--
Steven
More information about the Python-ideas
mailing list