[Python-Dev] Dict constructor

Tim Peters tim@zope.com
Wed, 26 Jun 2002 16:07:03 -0400


[Raymond Hettinger]
> I've been thinking about this and the unabiguous explicit solution is to
> specify a value argument like dict.get().
>
> >>> dict([(42, 666)])           # current behavior unchanged
> {42: 666}
>
> >>> dict([(42, 666)], True)
> {(42, 666): True}
>
> >>> dict( '0123456789abcdef', True)
> {'a': True, 'c': True, 'b': True, 'e': True, 'd': True, 'f': True, '1':
> True, '0': True, '3': True, '2': True, '5': True, '4': True, 7':
> True, '6': True, '9': True, '8': True}
>
> >>> dict('0123456789abcdef')    # current behavior unchanged
> ValueError: dictionary update sequence element #0 has length 1; 2 is
> required

That's better -- but I'd still rather see a set.

> The goal is not to provide full set behavior but to facilitate the
> common task of building dictionaries with a constant value.

The only dicts with constant values I've ever seen are simulating sets.

> It comes up in membership testing and in uniquifying sequences.

Those are indeed two common examples of using dicts to get at set
functionality.

> The task of dict() is to construct dictionaries and this is a
> reasonably common construction.

But only because there isn't a set type.