Dictionary from list?

James_Althoff at i2.com James_Althoff at i2.com
Tue Oct 23 13:13:32 EDT 2001


Russell E. Owen wrote:
>For what it's worth, I'm bullish on Chris Barker's suggestion of some
>way of converting list of keys, list of values to a dictionary. Like
>Chris, I've written my own and use it a fair bit.
>
>I have no opinion about converting the two proposed flavors of single
>lists to dicts (i.e. [key1, value1, key2, value2...] vs. [(key1,
>value1), (key2, value2)...]; they both sound useful, are both easy to
>implement via user-written functions, and if either is implemented as a
>built-in, then folks are likely to have a cow about the missing one.

Well, not to beat a dead cow, but (or cow butt, if you prefer) ...

I say take advantage of class methods (new in 2.2) and use them as
alternative constructors (factory methods, if you will) instead of badly
overloading the nominal constructor, as in (picking your own favorite
names, of course):

d1 = dictionary.fromKeyValueSeq([key1, value1, key2, value2...])

d2 = dictionary.fromKeyValuePairs([(key1, value1), (key2, value2)...])

etc.

(i.e., use the well known "eat a cow and have a cow, too" design pattern).

Jim






More information about the Python-list mailing list