[Python-Dev] Dict constructor

Barry A. Warsaw barry@zope.com
Wed, 26 Jun 2002 09:21:24 -0400


>>>>> "RH" == Raymond Hettinger <python@rcn.com> writes:

    RH> Second wild idea of the day:

    RH> The dict constructor currently accepts sequences where each
    RH> element has length 2, interpreted as a key-value pair.

    RH> Let's have it also accept sequences with elements of length 1,
    RH> interpreted as a key:None pair.

None might be an unfortunate choice because it would make dict.get()
less useful.  I'd prefer key:1

But of course it's fairly easy to construct either with a list
comprehension:

Python 2.2.1 (#1, May 31 2002, 18:34:35) 
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> abc = string.letters[:26]
>>> dict([(c, 1) for c in abc])
{'a': 1, 'c': 1, 'b': 1, 'e': 1, 'd': 1, 'g': 1, 'f': 1, 'i': 1, 'h': 1, 'k': 1, 'j': 1, 'm': 1, 'l': 1, 'o': 1, 'n': 1, 'q': 1, 'p': 1, 's': 1, 'r': 1, 'u': 1, 't': 1, 'w': 1, 'v': 1, 'y': 1, 'x': 1, 'z': 1}
>>> dict([(c, None) for c in abc])
{'a': None, 'c': None, 'b': None, 'e': None, 'd': None, 'g': None, 'f': None, 'i': None, 'h': None, 'k': None, 'j': None, 'm': None, 'l': None, 'o': None, 'n': None, 'q': None, 'p': None, 's': None, 'r': None, 'u': None, 't': None, 'w': None, 'v': None, 'y': None, 'x': None, 'z': None}

pep-274-ly y'rs,
-Barry