Dictionary from list?

Terry Reedy tjreedy at home.com
Tue Oct 23 18:53:05 CEST 2001


"Tim Peters" <tim.one at home.com> wrote in message
news:mailman.1003813882.5893.python-list at python.org...
> I appreciate that.  However, the builtin dictionary() cow has
already
> escaped the 2.2 barn, so the question now is whether we let it roam
the
> Python pasture with two broken legs, or put a spiffy sequence saddle
on it
> so you can gallop on it in comfort into the mooooonlight.

Fact: a Python dictionary literal is a sequence of key:value pairs of
literals, with the first becoming a hashable object, and only such a
sequence.

Observation: ':' is analogous to '( , )', which could have been the
syntax chosen (though I'm glad it wasn't).

Therefore:

Proposed rule 1: the dictionary() constructor should accept a sequence
of pairs of objects, with the first being keyable (hashable).  In
particular, it should invert dict.items.

Note: by type, 'pair' might mean duple only; by interface, it would
mean an object reporting a length of two and yielding objects with
indexes 0 and 1.

Comment: once pair is defined, this rule gives a uniform target for
conversion from other formats, including those generated by other
software systems.  I currently vote for the latter.

Proposed rule 2: dictionary() should reject any other sequence, just
as does the internal constructor-from-literals.

Paraphrase: conversions from the many other possible formats should be
handled externally from dictionary().

Opinion 3: Given the fact and comments above, these two rules should
be easy to understand and  teach.

Terry J. Reedy






More information about the Python-list mailing list