[Python-ideas] Dict view on list of two-tuples

Ian Bicking ianb at colorstudy.com
Mon Apr 4 20:42:29 CEST 2011


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).

For instance:

d = multidict([('a', '1'), ('b', '2'), ('a', '3')])

There are some open questions:

d['a'] -- '1' or '3'?
d.items() -- could be [('a', '1'), ('b', '2'), ('a', '3')], or [('b', '2'),
('a', '3')]
how do you get all values for 'a'?  (e.g., d.getall('a') == ['1', '3'])
what does "del d['a']" do?
what does "d['a'] = '4'" do?  It may overwrite or add a value.  Both are
valid -- how do you do the other.
d.pop() etc need specifying.
does d represent a view or a copy of the list?  I.e., are updates to the
list reflected in d?

Some implementations only represent ordering of individual values, e.g.,
they'll show that '3' comes after '1'.  But I need a complete ordering, that
is, I need to know the key ordering is 'a', 'b', 'a' (it does not
necessarily need to be shown through d.items(), but some method should
expose this).  Data structures with an internal representation like {'a':
['1', '3'], 'b': ['2']} are not sufficient.

For reference, my own opinion of an implementation:
https://bitbucket.org/ianb/webob/src/tip/webob/multidict.py#cl-13

  Ian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110404/b1589aed/attachment.html>


More information about the Python-ideas mailing list