Dictionary .keys() and .values() should return a set [with Python 3000 in mind]

Simon Forman rogue_pedro at yahoo.com
Sun Jul 2 12:48:45 EDT 2006


Nick Vatamaniuc wrote:
> Robert Kern wrote:
> > vatamane at gmail.com wrote:
> > > The same thing goes for the values(). Here most people will argue that
...
> >
> > This part is pretty much a non-starter. Not all Python objects are hashable.
...
> > Also, I may need keys to map to different objects that happen to be equal.
> >
> > --
> > Robert Kern
> >


So, values() can't return a set because of (at least) the two reasons
given above.  And since, as Scott David Daniels pointed out, dicts
support the iterator protocol, you can ask for a set of the keys easily
enough if you want it:

>>> d = dict(a=1, b=2, c=3)
>>> set(d)
set(['a', 'c', 'b'])

So, IMHO, there's not much point to having keys() return a set.


Peace,
~Simon




More information about the Python-list mailing list